Skip to content

refactor(stage3): select over execution contexts: x_{κ,v}, y_{κ,u,c,i}, h_v and constraint (R) (#1207 PR 7) - #1237

Merged
zzylol merged 23 commits into
mainfrom
refactor/1207-contexts
Nov 16, 2026
Merged

zzylol merged 23 commits into
mainfrom
refactor/1207-contexts

Conversation

@zzylol

@zzylol zzylol commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: PR 6's selection runs each candidate at most once, at the combined rate of every query reaching its group, and sets retention after solving; and a query may read, through another query's plan, a sketch of a group Pass 2 lists as exact-only for it (exact_only_groups_of_query was computed but not enforced).
  • Change: Stage 3 selects over execution contexts (concepts doc §5 "Execution contexts, by example", "Cost and retention"): $x_{\kappa,v}$ per execution, $y_{\kappa,u,c,i}$ per edge in the consumer's context, and retention as a decision $h_v$ with constraint (R); and each query reads its exact-only groups through exact candidates only, in its own selection context (constraint (X), per-query reads $a_{q,e}$).
  • Effect (review round 1 included): p99 every 10 min and p50 every 30 min over the last hour run one materialized chain read by one TimeRange per context (retention_ms 3,600,000, chosen by (R)); quantile_over_time(0.9, m[5m]) at relative 0.05 no longer reads the rank-0.01 query's KLL, and an exact p90 no longer reads a sketch; the MILP's optimum (Selection::selected_plan_cost_per_second) equals plan_cost on every objective pin (q21 0.9020/s) and on every planned harness workload (round 1: 98 of 98, round 2: 25 of 25, this PR's review: 43 of 43); an @ end() / @ start() subtree runs once per request in the MILP and in plan_cost alike (two queries sharing m[5m] @ end(): 0.0473/s, where plan_cost charged 0.1513/s); z05 (count_over_time(m[1m]) every 10 s + max_over_time(m[1d]) every 600 s) costs 0.9301/s, the brute-force optimum (was 1.0590/s); c16 plans in about 10 s (128 s before review round 4) and RangeHeavy-8users in about 9 s (222 s), because a copy of an equivalent evaluation context shares the original's MILP variables (round 5 fixed which contexts count as copies, H1: two contexts whose queries read different exact-only groups were wrongly copies when an AcrossEvaluations candidate answered first); an ingestion-time stored result refreshes, delivers and is costed on one rule, the installed plan's refresh window (M4); a query-time Scan is no longer costed for a reader it does not have (PR 6's F2 (a)): count_over_time(m[1m]) every 60 s next to max_over_time(m[1d]) costs 0.0062/s, as alone; Pass 2 no longer makes a count's input exact-only, so avg by (job)(quantile_over_time(0.99, data[300s])) at relative 0.05 over non-negative data plans again without raw data, as on main (round 5, F1: 30 random-mixed workloads now plan, 6 others cost 8–18% less); a stored result retains at least its newest output, as plan_cost charges.

Review guide

  • Position: PR 7 of the Tracking: implement the optimization formulation (memo candidates, properties, edges y_{u,c,i}, MILP) as named code per stage #1207 work, against main, rebased onto 59365ce80 (with fix(frontend): name every aggregate measure and validate names match measures #1308; PR 6 refactor(stage3): baseline selection MILP and its PhysicalASAPDAG; executor θ, native chains and a time-indexed store (#1207 PR 6) #1236 and fix(types): a stored result delivers on every context only when final; @ T pins only its own time (#1264) #1299 below it); refactor(stage3): constraint (K) per execution and metric; sign-conditioned DDSketch guarantee, classic HLL certificate from the source's distinct bound (#1207 PR 8) #1238 (PR 8, accuracy) builds on it. Head 0b5ec4653. Commits: the PR 7 model and (X) (d233d7323, 7060891de), review round 1 (c86e08497 … 527f80b11), R1 (e054797f3), review round 2 (571581460, 51b17a046, f144b76b7), review round 3 (69d205a2b, d96f41ec8), layering (a040fe2b1, 4cca6a110), review round 4 (0d04e4d90 context copies, 813ecaef5 M4), review round 5 (rebased: 6ca90a0ac, 3417d63be; before the rebase 21e473690, 796d43503), the round-5 reviews (44a223d93), the round-6 review (0b5ec4653). Backups of previous heads: backup/1237-1ef03212f, backup/1237-baab8969a, backup/1237-fa47deaab, backup/1237-c7465133b, backup/1237-33e7d6d56, backup/1237-77f403791, backup/refactor/1207-contexts-4cca6a110, backup/refactor/1207-contexts-813ecaef5, backup/refactor/1207-contexts-21e473690, backup/refactor/1207-contexts-796d43503.
  • Reading order:
    1. stage3-plan-selection/src/selectable.rs: SelectableExecutions::new (contexts, executions, edges by execution position, answering), input_context (request contexts per anchor); the rules it reads live in asap-types: ExecutionContext::EVERY_ARRIVING_ROW (types/src/stages/physical/execution_context.rs) and PhysicalProperties::has_one_reader_per_context (types/src/stages/physical/properties/mod.rs).
    2. stage3-plan-selection/src/cost.rs: execution_costs ($\pi_v$ per second retained, needs per edge, a scan's read on its edges), executions_per_second; retention.rs: edge_retention_need_ms, edge_scan_read_ms.
    3. stage3-plan-selection/src/milp/model/mod.rs (variables per execution, kept, the scale), milp/retention.rs ($h_v$, (R)), milp/exact_only.rs ((X)), milp/upper_bound.rs (per-query greedy bound with retention, dominated_by_one_evaluation_twin), milp/graph.rs.
    4. cost/src/plan/execution_demand/mod.rs (context_executions_per_second, computations per context, Consumer::QueryRequest { at }), cost/src/node/usage/mod.rs (raw rows materialized at ingestion, chain_tumbling_window_width_ms).
    5. types/src/stages/physical/memo_candidates/mod.rs: delivers_on (moved from PhysicalCandidate), stored_times, the candidates' TimeIndexedPlan; types/src/plan/materialized_view/mod.rs: RefreshWindow::computed_from; types/src/plan/time_window.rs: shift_back_ms_below over alternatives.
    6. stage3-plan-selection/src/milp/equivalent_contexts.rs: copies_in_equivalent_contexts (SharedVariables, contexts bucketed by ContextKey), SelectableExecutions::evaluation_context, and selection_variables in milp/model/mod.rs.
  • Mechanical: plan.rs (one node per selected execution, retention_ms from $h_v$), lib.rs/report.rs (executions instead of candidates), tests in sibling files (execution_demand/tests.rs, cost/tests.rs, report/tests.rs), the objective pins in stage3_objective.rs.
  • Careful: the exchange argument in copies_in_equivalent_contexts (which contexts count as copies); the request contexts in input_context; the one shared rate function; $h_v$ only for kept candidates and the scale over kept coefficients; the twin-dominance rule; one ingestion context for every ingestion-time execution that is not materialized.

File organization (by crate and scope)

crates/types/src/ir/operator/node.rs               Operator::{chain_tumbling_window_width_ms, request_anchor, keeps_input_window}, each exhaustive
crates/types/src/stages/logical/                   EvaluationGrid; query_must_not_read; Memo::output_groups
crates/types/src/stages/physical/memo_candidates/mod.rs  delivers_on, stored_times, is_query_time_scan, TimeIndexedPlan over candidates
crates/types/src/plan/materialized_view/mod.rs     RefreshWindow::computed_from (the one refresh rule); From<RefreshWindow> for EvaluationGrid
crates/types/src/plan/time_window.rs               shift_back_ms_below over alternative producers
crates/types/src/stages/physical/execution_context.rs  EVERY_ARRIVING_ROW, is_outside_evaluations; queries_of_context, anchor_is_evaluation_time, query_reads
crates/types/src/workload/query.rs                 QueryWorkloadEntry::{evaluation_grid_ms, evaluation_grid, request_time_grid_ms, range_span_ms}
crates/types/src/plan/materialized_view/raw_data.rs   keeps_raw_rows_at_ingestion
crates/types/src/stages/physical/properties/mod.rs is_across_evaluations
crates/cost/src/plan/execution_demand/{mod,tests}.rs  context_executions_per_second; computations per execution context
crates/cost/src/node/usage/{mod,tests}.rs           raw rows materialized at ingestion; chain width through operators that keep their input window
crates/cost/src/plan/cost.rs                       plan_cost: a node's producers in its payload's input order
crates/stage3-plan-selection/src/
  selectable.rs          contexts, executions, edges and answering executions a plan may select; QueryId
  cost.rs (+ cost/tests.rs)  per-execution, per-edge and retention costs, needs per edge
  retention.rs           need_{κ,u,c,i} and a scan's read per edge
  plan.rs                one node per selected execution
  report.rs (+ report/tests.rs)  Selection, infeasibility over executions
  milp/model/{mod,tests}.rs  variables and constraint families over executions
  milp/retention.rs      h_v and constraint (R)
  milp/exact_only.rs     a_{q,e} and constraint (X)
  milp/graph.rs          strongly connected components over executions
  milp/upper_bound.rs    per-query greedy bound over executions
  milp/dominance.rs      OneEvaluation-twin dominance
  milp/equivalent_contexts.rs  copies of equivalent evaluation contexts (symmetry reduction), SharedVariables
crates/accuracy/src/metric_rule/{mod,tests}.rs   the metric rule per input, error_metrics_allowed_on_input: exact-only lists and edges; a count's input is not exact-only
crates/accuracy/src/error_contract/mod.rs        ErrorContract::input_error_reaches_output (also used by readout_caps)
crates/types/src/ir/operator/non_asap.rs         map_children applies f in children() order (+ a test over every operator)
crates/types/src/ir/scalar/mod.rs                ScalarExpr::map_parts maps an IN's subquery first, as operator_refs lists it
crates/planner/tests/exact_only_reads.rs   (X) end to end
crates/integration-tests/tests/stage3_objective.rs  objective == plan_cost pins

Behavior: before → after

Planner test runtime_install::one_chain_serves_two_evaluation_contexts: two queries over the last hour of 1,000 latency series (1 sample/s), rank error 0.01, p99 every 10 min and p50 every 30 min, phase 0; the deployment keeps no raw data.

Before (PR 6) After (this PR)
Executions one run per candidate at the summed rate of all reaching queries one run per context that needs it (κ600, κ1800), once in total in Materialized(chain)
Retention set after solving from the selected consumers $h_v$ chosen under (R), costed in the objective: chain retention_ms 3,600,000
Plan one node per candidate one node per selected execution: one chain, two TimeRange(3600s) nodes (one per context), two roots

Planner tests exact_only_reads (the PR 6 review's repros):

Workload (every 60 s, 1,000 series of m) Before After
quantile_over_time(0.9, m[5m]) at rank 0.01 (Q0) and at relative 0.05 (Q1) Q1 reads Q0's KLL Q1 reads no KLL
the same p90 exact (Q0) and at rank 0.01 (Q1) Q0 reads Q1's KLL readout Q0 reads no sketch

Interfaces

ir/operator/node.rs · stages/logical/memo_candidates.rs · stages/physical/execution_context.rs · plan/materialized_view/raw_data.rs · workload/query.rs · stages/physical/memo_candidates/mod.rs · plan/materialized_view/mod.rs · plan/materialized_view/views.rs · plan/time_window.rs

 // asap-types, IR
 impl<C> Operator<C> {
-    /// `f` may change the reference type, e.g. from `Rc<OperatorNode>` to a node id.
+    /// `self` with `f` applied to each child, in [`Self::children`] order (direct inputs, then the
+    /// nodes its scalar expressions read), so a caller may hand out replacements by position.
     pub fn map_children<D>(&self, f: impl FnMut(&C) -> D) -> Operator<D>;
+    /// Its tumbling-window width when it is a `SummaryTumblingWindowChain`.
+    pub fn chain_tumbling_window_width_ms(&self) -> Option<u64>;
+    /// The anchor of a `TimeShift` with `@ end()` / `@ start()`; None otherwise (`@ <ts>` included).
+    pub fn request_anchor(&self) -> Option<AtModifier>;
+    /// Whether each output row keeps its input row's window: `Filter`, `Project`, `TimeShift`.
+    pub fn keeps_input_window(&self) -> bool;
 }
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
 pub enum AtModifier { Start, End, Timestamp(i64) }
 // asap-types, workload
+impl QueryWorkloadEntry {
+    /// The grid in ms its evaluations fall on; 1 when not known.
+    pub fn evaluation_grid_ms(&self) -> u64;
+    /// The grid in ms of its request ends (`@ end()`) or starts (`@ start()`); 1 when not known.
+    pub fn request_time_grid_ms(&self, at: AtModifier) -> u64;
+    /// Its range-evaluation span in ms, 0 for one evaluation per run.
+    pub fn range_span_ms(&self) -> u64;
+    /// The evaluation grid of its context: every `evaluation_grid_ms`, phase 0 (was Stage 3's free
+    /// `evaluation_grid(entry)`).
+    pub fn evaluation_grid(&self) -> EvaluationGrid;
+}
+/// One second, in ms (moved from asap-cost; `EVERY_ARRIVING_ROW` is one second wide).
+pub const ONE_SECOND_MS: u64 = 1_000;
 // asap-types, logical stage (EvaluationGrid moved here from stages::physical, re-exported from stages)
 pub struct EvaluationGrid { pub period_ms: u64, pub phase_ms: u64 }
+impl LogicalASAPMemoCandidates {
+    /// Whether `query` must not read `candidate`: it has θ and its group is on the query's exact-only list.
+    pub fn query_must_not_read(&self, query: usize, candidate: CandidateForMemoGroupId) -> bool;
+}
+impl Memo { pub fn output_groups(&self) -> Vec<(usize, MemoGroupId)>; }
-/// Each operator reference of `query_roots` (now `QueryRoot::operator_refs` in `output_groups`).
-pub fn output_references<C: Clone>(query_roots: &[QueryRoot<C>]) -> Vec<(usize, C)>;
 // asap-types, physical stage
 impl PhysicalCandidate {
-    /// Whether a consumer in `context` can read this producer.
-    pub fn delivers_on(&self, candidates: &PhysicalASAPMemoCandidates, context: &ExecutionContext) -> bool;
 }
 impl PhysicalProperties {
+    pub fn is_across_evaluations(&self) -> bool;
+    /// At most one consumer per context: query time and ℓ = None.
+    pub fn has_one_reader_per_context(&self) -> bool;
 }
 impl PhysicalASAPMemoCandidates {
+    /// The operator of candidate `v`'s logical candidate.
+    pub fn operator(&self, v: PhysicalCandidateId) -> &Operator<MemoGroupId>;
+    /// The times `v`, materialized across evaluations, has a stored result for: a chain at either
+    /// timing and any ingestion-time result on its refresh window (`RefreshWindow::computed_from`,
+    /// width as period), any other query-time result on its group's evaluation grid.
+    pub fn stored_times(&self, v: PhysicalCandidateId) -> Option<EvaluationGrid>;
+    /// Whether `v` is a `Scan` run at query time (moved from Stage 3's cost.rs).
+    pub fn is_query_time_scan(&self, v: PhysicalCandidateId) -> bool;
+    /// Whether a consumer in `context` can read producer `v` (moved from `PhysicalCandidate`,
+    /// whose data it does not hold); `Materialized(u)` uses u's stored times; finality from main.
+    pub fn delivers_on(&self, v: PhysicalCandidateId, context: &ExecutionContext) -> bool;
 }
+/// The candidates as the time walks see them: admissible producers as inputs, admissible
+/// consumers as readers; Stage 3's per-edge view delegates to it.
+impl TimeIndexedPlan for PhysicalASAPMemoCandidates { type Node = PhysicalCandidateId; type Child = MemoGroupId; /* ... */ }
 impl ExecutionContext {
+    /// Every ingestion-time execution that is not materialized: each second of arriving rows.
+    pub const EVERY_ARRIVING_ROW: Self;
+    /// `IngestionPipeline` or `Materialized`: every evaluation context reading its executions
+    /// shares them.
+    pub fn is_outside_evaluations(&self) -> bool;
 }
+/// The queries on evaluation grid `grid_ms` among `reaching`.
+pub fn queries_of_context(grid_ms: u64, reaching: impl IntoIterator<Item = usize>, entries: &[QueryWorkloadEntry]) -> Vec<usize>;
+/// Whether an `@ end()` / `@ start()` input read by `queries` runs in their evaluation context
+/// (`@ end()` with an instant query among them).
+pub fn anchor_is_evaluation_time<'e>(at: AtModifier, queries: impl IntoIterator<Item = &'e QueryWorkloadEntry>) -> bool;
+/// How each query reads the nodes below it, per (query, anchor), over a plan or a memo; Stage 3 and
+/// `plan_cost` rate a context by it.
+pub fn query_reads<N: Copy + Eq + Hash>(starts: &[Vec<N>], inputs_of: impl Fn(N) -> Vec<N>,
+    anchor_of: impl Fn(N) -> Option<AtModifier>, stops_at: impl Fn(N) -> bool, entries: &[QueryWorkloadEntry])
+    -> HashMap<N, Vec<(usize, Option<AtModifier>)>>;
 // asap-types, plan
 impl RefreshWindow {
+    /// The one refresh rule of an ingestion-time stored `node` in any `TimeIndexedPlan` (plan
+    /// nodes, Stage 2's output `PhysicalASAPMemoCandidates`): the union of the windows it is
+    /// computed from; a chain of width w over shifts s is (w, s mod w).
+    pub fn computed_from<P: TimeIndexedPlan>(plan: &P, node: P::Node) -> Option<Self>;
 }
+/// The times a refresh window ends at: period = width, same phase (next to `RefreshWindow`).
+impl From<RefreshWindow> for EvaluationGrid;
 impl PhysicalASAPDAG {
     /// `RefreshWindow::computed_from` over this DAG.
     pub fn refresh_window(&self, node: PhysicalASAPNodeId) -> Option<RefreshWindow>;
-    /// `refresh_window` of each of `nodes`, the DAG indexed once.
-    pub fn refresh_windows(&self, nodes: impl IntoIterator<Item = PhysicalASAPNodeId>) -> HashMap<PhysicalASAPNodeId, Option<RefreshWindow>>;
 }
 /// Now follows every input (over candidates, the alternative producers of one input); the paths
 /// must agree, else `None`.
 pub fn shift_back_ms_below<P: TimeIndexedPlan>(plan: &P, node: P::Node) -> Option<u64>;
 impl PhysicalASAPDAGNode {
+    /// A `Scan`, or a `TimeShift` without `@` over one (`input`), stored across evaluations at ingestion.
+    pub fn keeps_raw_rows_at_ingestion<C>(&self, input: Option<&Operator<C>>) -> bool;
+    /// The nodes it reads, one per edge into it in a validated DAG, in its payload's input order
+    /// (`Operator::children`: direct inputs, then each node its scalar expressions read, once).
+    pub fn producers(&self) -> Vec<PhysicalASAPNodeId>;
 }

crates/cost/src/plan/execution_demand/mod.rs · crates/cost/src/node/usage/mod.rs

 pub enum Consumer {
     Query(usize),
+    /// Query `query` below a `TimeShift` anchored with `at`: once per request.
+    QueryRequest { query: usize, at: AtModifier },
     Materialized(PhysicalASAPNodeId),
 }
+/// The run rate of a node in one context of `queries` (Stage 3's and `plan_cost`'s one function):
+/// no anchor, at their evaluations; `@ end()`, once per request end; `@ start()`, per span, summed.
+pub fn context_executions_per_second<'e>(anchor: Option<AtModifier>,
+    queries: impl IntoIterator<Item = &'e QueryWorkloadEntry>, horizon_seconds: f64) -> Result<f64, AnalyticalCostError>;
 /// Signature changed: the workload entries, for `query_reads`.
-pub fn consumers(dag: &PhysicalASAPDAG) -> HashMap<PhysicalASAPNodeId, Vec<Consumer>>;
+pub fn consumers(dag: &PhysicalASAPDAG, entries: &[QueryWorkloadEntry]) -> HashMap<PhysicalASAPNodeId, Vec<Consumer>>;
+/// The time between two outputs of a node that keeps rows (π_v's Δ in Stage 3).
+pub fn output_interval_ms<C>(payload: &Operator<C>, window_ms: Option<u64>) -> u64;
-/// The window an ingestion-time grouping maintains (the first range or chain below it); replaced
-/// by the refresh window's width in `plan_cost` and Stage 3.
-pub fn maintained_window_ms<'o, N: Copy + Ord, C: 'o>(node: N, operator_of: impl Fn(N) -> &'o Operator<C>,
-    input_of: impl Fn(N) -> Option<N>) -> Option<u64>;
+/// An ingestion-time reader of a raw-row store reads one batch; Stage 3 and `plan_cost` both use it.
+pub fn reads_one_batch_of<C>(consumer: &PhysicalASAPDAGNode, producer: &PhysicalASAPDAGNode,
+    producer_input: Option<&Operator<C>>) -> bool;
 pub struct ProducerOutput<'a> { ..., pub tumbling_windows: u64,
+    pub chain_tumbling_window_width_ms: Option<u64> }
 pub struct NodeResourceUsageAndCost<'a> { ...,
+    pub chain_tumbling_window_width_ms: Option<u64> }
 /// Behavior: a node's producers are `PhysicalASAPDAGNode::producers` (its payload's input order),
 /// not the DAG's edge order.
 pub fn plan_cost(dag, entries, data, settings) -> Result<(PlanCost, CalibrationFit), ..>;

asap-accuracy (error_contract/mod.rs · metric_rule/mod.rs):

+impl ErrorContract {
+    /// Whether any of input `input`'s error reaches the output (β_{c,i} > 0); a `count`'s does not.
+    pub(crate) fn input_error_reaches_output(&self, input: usize) -> bool;
+}
 impl ErrorMetricsOfMemo {
-    pub(crate) fn error_metrics_allowed_on_inputs(&self, consumer) -> Option<Vec<AccuracyMetric>>;
-    fn error_metrics_passed_to_inputs(&self, consumer, metrics: &[AccuracyMetric]) -> Vec<AccuracyMetric>;
+    /// The metric rule: (output_allowed ∩ the metrics the contract passes) ∪ the metrics whose
+    /// error does not reach the output. The exact-only walk, its closure and the edges use it.
+    pub(crate) fn error_metrics_allowed_on_input(&self, consumer, input: usize,
+        output_allowed: &[AccuracyMetric]) -> Vec<AccuracyMetric>;
+    /// `error_metrics_allowed_on_input` at what the consumer's group output allows; `None` where
+    /// no query with a target reaches it.
+    pub(crate) fn error_metrics_allowed_on_input_edge(&self, consumer, input: usize)
+        -> Option<Vec<AccuracyMetric>>;
 }

Crate-private, Stage 3 (selectable.rs · cost.rs · retention.rs · milp/):

 // selectable.rs
+pub struct QueryId(pub usize);
+pub(crate) struct Execution { pub(crate) context: ExecutionContextId, pub(crate) candidate: PhysicalCandidateId }
+pub(crate) struct SelectableEdge { pub(crate) consumer: usize, pub(crate) edge: PhysicalEdgeId, pub(crate) producer: usize }
+pub(crate) enum Reader { Edge(usize), Output(QueryId) }
+pub(crate) struct SelectableExecutions { /* contexts, request anchors, context ids, reads per memo group,
+    executions, edges, edges_into, edges_out_of, answering: all private */
+    /// Per query, the evaluation context it is evaluated in (no request anchor); None without an output.
+    evaluation_context_of_query: Vec<Option<ExecutionContextId>>,
+}
+impl SelectableExecutions {
+    pub(crate) fn new(physical, entries, outputs: &[(QueryId, MemoGroupId)]) -> Self;
+    pub(crate) fn readers_of(&self, group, anchored: impl Fn(Option<AtModifier>) -> bool) -> BTreeSet<usize>;
+    pub(crate) fn retain_answering(&mut self, keep);
+    pub(crate) fn reads_back(&self, need_ms_per_edge, entries) -> impl Iterator<Item = (usize, Reader, Option<u64>)>;
+    pub(crate) fn evaluation_context(&self, query: QueryId) -> Option<ExecutionContextId>;
+    /// `Evaluation` with no request anchor.
+    pub(crate) fn is_root_evaluation_context(&self, id: ExecutionContextId) -> bool;
+    // context, request_anchor, executions, edges, edges_into, edges_out_of, answering, input_count
+}
 // cost.rs
+pub(crate) struct ExecutionCosts { per_execution, per_edge, retention_cost_per_retained_second,
+    retention_need_ms_per_edge, newest_output_retention_ms, least_retention_ms_per_execution, raw_data_source,
+    unbounded_raw_data_on_disk_cost_per_second }
+impl ExecutionCosts {
+    pub(crate) fn retention_cost_per_second(&self, v, retention_ms: u64) -> f64;
+    /// What `v` retains for a read needing `need_ms`: at least its newest output (Δ).
+    pub(crate) fn retention_ms_for_need(&self, v, need_ms: u64) -> u64;
+}
 // retention.rs
+pub(crate) fn edge_retention_need_ms(physical, entries, edge) -> Option<u64>;
+pub(crate) fn edge_scan_read_ms(physical, edge) -> Option<u64>;
+/// The least retention over each execution's costed reads, each read's need at least its
+/// candidate's newest output (`retention_ms_for_need`).
+pub(crate) fn least_retention_ms_per_execution(selectable, costs: &ExecutionCosts, entries) -> Vec<Option<u64>>;
+pub(crate) fn ms_to_seconds(ms: u64) -> f64;
 // milp
 pub(crate) struct SelectionInputs<'a> { physical, entries, selectable: &SelectableExecutions, costs: &ExecutionCosts }
 pub(crate) struct SolvedSelection { executions: Vec<Execution>, edges: Vec<SelectedEdge>, outputs: Vec<Execution>,
     retention_ms: HashMap<PhysicalCandidateId, u64>, cost_per_second_without_request_overhead: f64, .. }
+pub(crate) struct SelectedEdge { consumer: Execution, edge: PhysicalEdgeId, producer: Execution }
+// milp/retention.rs: h_v and (R), including h_v >= least need · x_u (F6′)
+// milp/exact_only.rs: a_{q,e} and (X)
+// milp/dominance.rs: dominated_by_one_evaluation_twin
+// milp/equivalent_contexts.rs: per execution, edge and query the original whose variables it
+// takes (itself when none), a copy of an equivalent evaluation context taking the original's:
+pub(crate) struct SharedVariables { pub(crate) original_of_execution: Vec<usize>,
+    pub(crate) original_of_edge: Vec<usize>, pub(crate) original_of_query: Vec<QueryId> }
+pub(crate) fn copies_in_equivalent_contexts(inputs: &SelectionInputs<'_>, kept: &[bool]) -> SharedVariables;
+// milp/mod.rs: one relative tolerance for dominance, the copies (of the largest cost the MILP
+// reads in the copy context: kept executions, edges between kept ones) and the greedy bound's
+// relaxation (1e-12)
+const COST_RELATIVE_TOLERANCE: f64;
+// milp/upper_bound.rs: dearer_than_a_feasible_plan (greedy bound, per-query exclusions, least retention)

Removals (items on main): in Stage 3, SelectionCosts/selection_costs, selectable_edges, query_outputs, the free evaluation_context, delivers_to_consumer and output_groups. In asap-cost, maintained_window_ms and the private ONE_SECOND_MS (now asap_types::workload::ONE_SECOND_MS). In asap-types, PhysicalCandidate::delivers_on (now on PhysicalASAPMemoCandidates), PhysicalASAPDAG::refresh_windows and stages::output_references. In asap-accuracy, ErrorMetricsOfMemo::{error_metrics_allowed_on_inputs, error_metrics_passed_to_inputs} (now error_metrics_allowed_on_input(_edge)).

Usage

The two review-round-4 changes, end to end, as the tests run them (cargo test -p asap-types a_stored_chain_and_an_ingestion_time_result_are_stored_on_their_refresh_window, cargo test -p asap-stage3-plan-selection a_context_equivalent_to_another_shares_its_variables) and as the CLI runs c16:

// The workload: topk(5, sum by (host)(rate(c[5m]))) every 60 s and the same query every 120 s
// (c16), planned with `asap_planner --workload c16.json --run-until stage3`.
// Stage 3, `select_plan`, builds one evaluation context per grid (κ60, κ120), each with 992
// executions of the same candidates, plus the scan's ingestion context and two materialized ones.
let selectable = SelectableExecutions::new(physical, entries, &outputs);
// Per execution its cost; every κ120 cost is half the κ60 one (half the runs).
let costs = execution_costs(physical, &selectable, entries, data, cost_inputs);
// In `solve_selection` -> `selection_variables`: κ120 is a copy of κ60. Per execution, edge and
// query, the original whose x, y, z it takes: κ120's 992 executions -> their κ60 twins.
let SharedVariables { original_of_execution, original_of_edge, original_of_query } =
    copies_in_equivalent_contexts(inputs, &kept);
// The MILP: 3,934 variables instead of 7,858 (rows 7,909 unchanged); the optimum
// 0.0501500010759250/s, the same as before; solve about 10 s instead of about 128 s.

// M4, Stage 3's ingestion-pipeline context and delivers_on: `sum(TimeRange(5m))` stored at
// ingestion time (candidate 2 over the range 1 over the scan 0), its group read every 60 s.
let window = RefreshWindow::computed_from(&candidates, PhysicalCandidateId(2));
// (300 s, 0): the window the installed plan refreshes it on (was the group grid, 60 s).
assert_eq!(window, Some(RefreshWindow { width_ms: 300_000, phase_ms: 0 }));
// So a 60 s reader may not read it, a 5 min one may; its inputs run in IngestionPipeline(300 s, 0),
// and the cost model charges one output per 300 s (`ingestion_window_ms`, `plan_cost`).
assert!(!candidates.delivers_on(PhysicalCandidateId(2), &evaluation(60_000, 0)));
assert!(candidates.delivers_on(PhysicalCandidateId(2), &evaluation(300_000, 0)));
// A 60 s chain over TimeShift(30s) (candidate 4): (60 s, 30 s), was (60 s, 0).
assert_eq!(RefreshWindow::computed_from(&candidates, PhysicalCandidateId(4)),
    Some(RefreshWindow { width_ms: 60_000, phase_ms: 30_000 }));

Review round 1: what changed

Functional, quality and efficiency reviews of 1ef03212f; all approved items are in.

Finding Change Evidence
F1: $h_v$ of a candidate whose executions were all left out stayed in the objective and set the scale (z05 1.0590 vs 0.9301/s) $h_v$ and its (R) rows only for a candidate with a kept execution; scale over the coefficients kept z05 0.9301/s, z03 0.1132/s, z08 0.1947/s, y15 0.1393/s (brute force: 0.930103, 0.113217); a_candidate_dearer_than_a_feasible_plan_does_not_hide_a_cheaper_plan (fails without the fix)
F2 + quality #1: two run-rate copies; plan_cost stopped at OneEvaluation nodes, so an @ subtree was charged per step (a07: objective 0.0473/s, plan_cost 0.1513/s) one function asap_cost::context_executions_per_second, called by Stage 3 and by computations; computations walks through OneEvaluation nodes; @ start() counts distinct request starts a07 objective = plan_cost; stage3_objective::the_objective_is_the_plan_cost_on_a_shared_at_end_subtree (fails on 1ef03212f), ..._on_at_start_subtrees_of_two_spans; execution_demand::tests::{a_shared_one_evaluation_at_end_shift_runs_its_input_once_per_request, at_start_inputs_run_once_per_distinct_request_start}
F3: an @ start() input shared one OneEvaluation result with instant evaluations (a10) and with @ end() (a04, a09) the inputs of an @ end() / @ start() shift run in a context of their own, keyed by the anchor and the queries that reach the shift; its rate counts those queries' requests only an_at_start_subtree_shares_no_evaluation_result_with_an_instant_query (fails on 1ef03212f); ..._on_anchored_and_plain_queries_on_one_grid. a04 0.1364/s, a09 0.0525/s, a10 0.0325/s, each = plan_cost (on 1ef03212f their plan_cost was 0.2353, 0.1514, 0.1313/s)
F4: the greedy bound left out of every query what some query must not read (x07, n24: no bound) each query's trees leave out what that query must not read; a reused tree is checked against the reader's (X) the_bound_leaves_out_of_each_query_only_what_it_must_not_read (fails with the union)
F5: ~2e-6 gaps on c01, n20, p02 closed by F1 equal or lower objectives on all of them
Efficiency F1/F2: the bound ignored retention the bound adds $\pi_v \cdot$ (least need over an execution's uses) to its own cost; (F) gives a selected execution a use and (R) then retains at least that the_bound_counts_the_least_retention_an_execution_pays
Efficiency F3: Lifetime::None / OneEvaluation twins a query-time None execution is left out when its OneEvaluation twin (same logical candidate and θ, same context) costs no more, reads each of its producers and is read by each of its consumers on the same input at no more, and answers every query output it answers; none with $h_v$ or raw data. Valid under PR 8's per-execution (K): the twins have the same θ and producers a_none_execution_its_one_evaluation_twin_dominates_is_left_out (left out at equal cost; kept when the twin costs more or cannot be read by its consumer)
PR 6's rounds 3 and 4 (#1236) (B) and (D) left out for a producer with one reader per context (its (F) equality implies them); the scale in the raw-data rows, $r_s$ with coefficient 1 rows' coefficients stay in about [1, 1e3]
With PR 6's retention rules: example 3a's objective was 4.68e7/s (and plan_cost 8.89e7/s): with #1236's retention of a TimeShift view including its offset, $\pi_v$ of an ingestion-time view was costed per second while it keeps one output per window it maintains, so $W^{\mathrm{mat}}_v$ was −6.6e12/s and set the scale $\pi_v$ is one more output's cost over the window an ingestion-time view maintains (ingestion_window_ms, shared with costing) example 3a 21,686.84/s = plan_cost; stage3_objective::the_objective_is_the_plan_cost_on_one_off_queries_over_years (fails without the fix)
D2: a query-time Scan's read per execution a not-materialized scan's read is on its edges (one reader per context makes it exact, no new variables); a materialized one keeps the most its readers in the context read objective = plan_cost on every planned harness workload
Quality #2 ExecutionCosts::retention_cost_per_retained_second ($\pi_v$ per second retained); the two ×1000 dropped
Quality #3 ExecutionCosts::retention_need_ms_per_edge, computed once per memo edge; (R), least_retention_ms and the bound read it
Quality #4 the per-candidate scan read kept only for the baseline output (baseline_scan_read_ms)
Quality #5 retention.rs: edge_retention_need_ms and edge_scan_read_ms over one read_back
Quality #6 edges by execution position and edges_into built once in SelectableExecutions; (A), the bound, graph.rs, exact_only.rs use them; index_of deleted
Quality #7, #8 PhysicalCandidate::own_schedule; PhysicalProperties::is_across_evaluations; has_one_reader_per_context shared by (F), (B)/(D) and the bound
Quality #9, #10 the request overhead added in select_plan; the forwarder deleted, asap-cost takes the workload entry; Consumer::QueryRequest { at }; the anchors looked up in a map
Quality #11 SelectableExecutions with answering as a field; milp/retention.rs; QueryId in selectable.rs
Quality #12, #13, #14 forbidden answering executions filtered in select_plan (no z <= 0 rows); the ingestion arm reachable once the rate matches on the context first; the test helper uses output_groups
Quality #15 planner-layering.md and asap-aware-mapping-architecture.md list exact_only.rs, milp/retention.rs and (X)

Planning time, before (1ef03212f) → after (this head), HiGHS seeds 0–4, median (min–max):

Workload 1ef03212f This head
c16 (topk(5, sum by (host)(rate(c[5m]))) every 60 s and 120 s; debug harness, wall) 1,202 s (841–1,552) 93 s (76–168)
c09 (debug harness, wall) 106 s (81–152) 28 s (25–28)
example 3a (release asap_planner, wall) 2.1 s (1.9–3.8) 0.4 s (0.4–0.6)
long-02 (release, wall) 64.7 s (46.7–73.0) 11.2 s (9.8–11.6)
long-07 51.5 s (29.1–61.5) 9.0 s (3.7–9.0)
long-10 44.2 s (35.5–71.1) 9.5 s (7.8–10.0)
x5-05 27.5 s (21.9–36.5) 5.5 s (5.3–6.1)
all 34 tools/dag-viewer examples, one run, seed 0 (release, wall) 927 s (the efficiency review's run; PR 6: 43 s) 141 s

Solve times are seed-dependent; the objective is the same for every seed (example 3a within 1.4e-10 relative). The host was shared with other jobs.

Review round 2: what changed

Finding Change Evidence
Functional N1: @ start() counted each distinct start time once, but two queries requested together with different spans need two runs (h02b: charged 1/120 s, needs 2/120 s) context_executions_per_second (the rate Stage 3 and plan_cost share) counts @ start() once per request of each distinct (schedule, span) execution_demand::tests::at_start_inputs_run_once_per_request_of_each_span (spans 1 h and 2 m every 60 s: 2/60, was 1/60; fails without the fix). h02b 0.00667 → 0.00927/s, h02c 0.00870 → 0.01130/s, k19 0.0812 → 0.1072/s, each = plan_cost
Quality N1 asap_cost::plan::execution_demand::queries_of_context: Stage 3 (queries reaching a memo group, or an anchored shift) and computations (queries whose walk reaches a plan node) pick a context's queries with one rule. Each side still reaches queries its own way (the memo group before solving, the plan after); objective = plan_cost on every workload below stage3_objective::the_objective_is_the_plan_cost_on_two_phases_of_one_grid
N2, N9 milp/dominance.rs holds dominated_by_one_evaluation_twin, over two lists (ℓ = None, OneEvaluation) per (context, logical candidate); type Twins deleted a_none_execution_its_one_evaluation_twin_dominates_is_left_out
N3 crate::retention::reads_back: every read of an execution's rows (edge needs and query-output spans); costing, (R), least_retention_ms, the greedy bound and F6′ fold it
N4 SelectableExecutions::edges_out_of next to edges_into; the components, (X) and dominance walk them
N5 SelectableEdge { consumer, edge, producer }; all fields private with getters (answering()); context ids in a HashMap
N6 retention::ms_to_seconds and ExecutionCosts::retention_cost_per_second; has_one_reader; the raw_data_on_disk doc says it is scaled
N7 raw_data_disk_rows gives the raw-data rows to the scale and to the constraints
N8 delivers_on compares with own_schedule (no % 0)
N10 per_execution as if/else if let
N11 asap-cost: the Scan arm and the retained raw rows share raw_rows
Efficiency F6′ per kept execution $u$ of a candidate with $h_v$: $h_v \ge \min_{\text{uses}} \operatorname{need} \cdot x_u$ (valid: (F) gives a selected execution a use, (R) then retains at least that) objectives equal with and without it on all 35 timed workloads and every harness workload

Re-verified on 33e7d6d56 (same tree as the round-2 head before the rebase onto main):

  • fmt and clippy (-D warnings) clean; cargo test --workspace: 1415 passed, 66 failed, the same 66 names as before.
  • Objective = plan_cost: round 1 98/98, round 2 25/25, this PR's set 43/43, the round-2 review's h/k set 32/32. Objectives equal to c7465133b except h02b, h02c, k19 (N1, above) and g05 (−6e-9). Executed answers: 53/54 (x16 is PR 8's), 25/25, 34/34, 11/11.
  • Brute force on every set: never cheaper than the MILP; only searches that hit their time limit remain (round 1: p29, x06, x10; this PR's set: x01, x03, x08, x11, x15; h/k: k10, where the partial search is dearer).

Timings with F6′ on and off on this head, release asap_planner, HiGHS seeds 0–4, median (min–max); solve times are seed-dependent:

F6′ off F6′ on
34 tools/dag-viewer examples, MILP solve (sum of medians) 129.7 s 16.4 s
34 examples, wall (sum of medians) 152.9 s 38.3 s
c09 (topk over rate, one context), wall 22.0 s (20.0–22.4) 18.8 s (8.6–30.5)
c16 (topk(5, sum by (host)(rate(c[5m]))) every 60 s and 120 s), wall 90.3 s (76.8–158.5) 136.5 s (86.7–280.1)

c16 is not helped by F6′: its time is the θ fan-out across two contexts (efficiency F4, a follow-up).

Review round 3 and layering: what changed

Finding Change Evidence
Rebase onto main with #1299 conflicts in physical/candidate.rs (3 commits) and memo_candidates/tests.rs (both sides add tests, both kept); #1299's finality rule kept in delivers_on with this PR's schedule; the Materialized(v) sentence says "no non-final producer" all tests pass
Q1 an ingestion-time view without a window is final (current at every time): it delivers on every context; its cost charges one output per second memo_candidates::tests::an_ingestion_time_view_without_a_window_delivers_on_every_context
N-k19 (functional) a run in an evaluation context is rated by the queries whose walk reaches its group in their evaluation context (asap_types::stages::query_reads, the walk plan_cost takes over the plan; Stage 3 walks memo groups), not by every query reaching the group; closes quality R3 k19_C 0.3642 → 0.1542/s = plan_cost; stage3_objective::the_objective_is_the_plan_cost_on_an_instant_and_a_shifted_range_at_end (fails without the fix); a03, a04, k01, k02, k07, k09, k10, x1–x3 lower, each = plan_cost
R1 @ start() rate: evaluation_rate of the request schedules per span, summed over spans; anchored_request_recurrence deleted at_start_inputs_run_once_per_request_of_each_span (with phases 10 s and 70 s of one schedule: 1/60)
R2, M5 ExecutionCosts::least_need_ms_per_execution, one function over costed reads, read by the greedy bound and F6′
R4 the bound uses has_one_reader
R5 the query-time Scan reads raw_rows_back
R6 Candidates holds its edge; readers_of in the trait impl
R7 module docs and planner-layering.md list dominance.rs
R8 the is_across_evaluations wrapper inlined; SolvedSelection::edges is Vec<SelectedEdge>
A, L8 Operator::chain_tumbling_window_width_ms (types, Stage 3, asap-cost, Pass 2); EvaluationGrid moves to the logical layer (round 4 moved delivers_on to PhysicalASAPMemoCandidates; round 5 deleted own_schedule)
M1 one rule for a raw-row store's ingestion reader: PhysicalASAPDAGNode::keeps_raw_rows_at_ingestion (a Scan, or a TimeShift without @ over one) and asap_cost::reads_one_batch_of, used by Stage 3 and now by plan_cost (which charged the reader the whole store) and the cost model's raw-row rule execution_demand::tests::an_ingestion_time_reader_of_raw_rows_kept_at_ingestion_reads_one_batch (fails without the fix); no harness workload selects such an edge
M2 asap_cost::output_interval_ms is public; Stage 3's Δ of π_v calls it objectives unchanged
M3 Operator::request_anchor
M6 no move; a stage3_objective case with two queries reading one anchored group the_objective_is_the_plan_cost_when_two_queries_read_one_anchored_group
L1–L5, L7, L9, L10 query_must_not_read, has_one_reader_per_context, ExecutionContext::EVERY_ARRIVING_ROW, QueryWorkloadEntry::{evaluation_grid_ms, request_time_grid_ms}, queries_of_context/anchor_is_evaluation_time/query_reads in types::stages (Stage 3 imports no context rule from asap-cost), PhysicalASAPMemoCandidates::operator, SelectableExecutions::reads_back, Memo::output_groups
M4 not changed; see "Decisions and open questions"

Review round 4: M4 and the context-doubling slowdown

Finding Change Evidence
M4 (decided (b), 2026-11-15) one rule for when an ingestion-time stored result refreshes: the installed plan's (RefreshWindow::computed_from: a chain over shifts s is (w, s mod w), anything else the union of the windows below it), over any TimeIndexedPlan. PhysicalASAPDAG::refresh_window, Stage 3's IngestionPipeline context, delivers_on (through stored_times) and both cost paths (plan_cost and Stage 3's ingestion_window_ms, the window's width) call it. own_schedule stayed for query-time results (round 5 deletes it, M-A); maintained_window_ms and refresh_windows are deleted memo_candidates::tests::a_stored_chain_and_an_ingestion_time_result_are_stored_on_their_refresh_window (sum(TimeRange(5m)) on a 60 s grid: (5 min, 0), no longer delivers on 60 s; a chain over TimeShift(30s): phase 30 s; fails on the old rule); 197 set and 34 examples: costs unchanged (table below)
Context doubling (6–60× on multi-grid workloads) root cause: the multi-grid workloads ask the same queries on several grids (one user every 60 s, another every 300 s; c16 one query on 60 s and 120 s). Each grid is an evaluation context with the same executions and edges, each cost a fixed multiple of the other's, and the MILP chose each copy freely, so HiGHS branched over both. A copy of an equivalent context now shares the original's x, y and z (copies_in_equivalent_contexts); exact to within 1e-12 of the largest kept coefficient (Algorithm changes) milp::model::tests::a_context_equivalent_to_another_shares_its_variables; timings below

Median over HiGHS seeds 0–4 (release, one thread; host load 30–90 from other jobs, before and after run side by side), MILP variables / rows as built, and the cheapest plan cost over the seeds:

Workload Wall before Wall after Variables Rows Cost per second before → after
c16 (one query, 60 s and 120 s) 136.3 s (86.6–350.0) 9.9 s (9.4–18.0) 7,858 → 3,934 7,909 → 7,909 0.0501500010759 → 0.0501500010759
RangeHeavy-8users, whole 205.7 s (18.6–587.0) 9.6 s (7.7–12.7) 9,088 → 4,777 10,728 → 10,728 9.52950391413e-05 → 9.52950391415e-05
RollupHeavy-8users, whole 30.3 s (26.1–44.3) 24.4 s (19.5–29.2) 15,630 → 8,003 20,770 → 20,770 2.14591587058e-05 → 2.14591587059e-05
RangeHeavy-8users L1 quantiles 11.6 s (6.7–52.7) 6.3 s (4.1–7.6) 7,272 → 3,701 8,427 → 8,427 9.11068707682e-05 → 9.11068707684e-05
RollupHeavy-8users L1 http-p99 11.3 s (8.1–14.7) 4.5 s (3.3–6.8) 5,636 → 2,899 7,593 → 7,593 1.59197829745e-06 → 1.5919782979e-06
RollupHeavy-8users L1 http-distinct 5.4 s (4.9–6.1) 3.4 s (2.6–5.5) 8,510 → 4,342 11,209 → 11,209 4.99379085643e-06 → 4.99379085444e-06
RollupHeavy-8users L1 flows 2.6 s (2.0–2.8) 0.8 s (0.7–1.4) 1,526 → 786 2,037 → 2,037 1.58684010325e-05 → 1.58684010325e-05
RangeHeavy-8users L1 rate 0.0 s (0.0–0.1) 0.0 s (0.0–0.1) 231 → 231 331 → 331 1.07601981688e-06 → 1.07601981688e-06
c09 (one grid, control) 19.9 s (8.5–30.8) 20.7 s (8.9–39.8) 3,934 → 3,934 3,960 → 3,960 0.00786667384083 → 0.00786667384083
RangeHeavy L1 quantiles (one grid, control) 5.2 s (3.5–6.9) 5.3 s (3.5–6.8) 3,731 → 3,731 4,322 → 4,322 9.11054493286e-05 → 9.11054493286e-05

Costs differ by at most 5e-10 relative, in both directions: HiGHS stops within its tolerances (the per-seed optima of one model already spread by up to 2e-6 relative on http-p99). On RangeHeavy-8users L1 quantiles (seed 0), the unshared model's own κ60 selection, copied into κ300, costs 1.42518739e-9/s there, less than the 1.42518742e-9/s it selected for κ300: the unshared optimum is itself a near-optimum within HiGHS's tolerance.

Both changes against 4cca6a110, seed 0: of the round-4 set of 197, 168 ran on both heads (the rest, random-mixed workloads with MVP caps and accuracy enforced that plan nothing on either head, were stopped), 142 plan on both, 141 at a bitwise-equal cost and o11y_all14 within 1.4e-16, none planned on one head only; the 34 tools/dag-viewer examples all plan, 28 bitwise equal and the rest within 1.9e-16 (46.9 s → 50.8 s wall, none multi-grid). So M4 changes no selected plan in these sets.

Review round 5: what changed

Reviews of 813ecaef5: abstraction, layering and functional (applied here); efficiency is still running and goes into the next round. Commits 21e473690 (abstraction, layering, #1330 alignment) and 796d43503 (functional F1, F2 (a), F3). Each fix's test was written first and fails on the code before it.

Finding Change Evidence
H1 (high): a query is put in the wrong context when its first answering execution is materialized SelectableExecutions::new records each query's evaluation context (evaluation_context_of_query, read by evaluation_context(q)); the copy search reads it and the answering.first() derivation is deleted; selection_variables debug-asserts that every shared $z$ key is a real (query, answering execution) pair contexts_whose_queries_read_different_groups_are_not_copies (on 813ecaef5: the two contexts shared 6 executions with the queries unpaired); queries_reading_different_groups_select_differently_in_contexts_alike (optimum 8; on 813ecaef5 9, Q0 forced onto the exact quantile); 197 set and 34 examples unchanged
M-A, L-3 stored_times uses RefreshWindow::computed_from for any chain, at either timing, and the group's evaluation grid for any other query-time result; own_schedule is deleted. A query-time AcrossEvaluations chain over TimeShift(30s) is stored at phase 30 s (was 0) a_stored_chain_and_an_ingestion_time_result_are_stored_on_their_refresh_window (query-time chain: (60 s, 30 s), delivers on 60 s @ 30 s and 120 s @ 90 s, not on 60 s @ 0)
L-A the out-edge and input-count checks are gone (implied by the key); out-edge counts are a debug_assert
L-B contexts are bucketed by a canonical ContextKey (per execution: candidate, kept, costed, least need, in-edges by memo edge and producer; sorted query signatures); only the cost proportionality check compares two contexts, and queries pair in signature order (no swap_remove matching) a_context_equivalent_to_another_shares_its_variables unchanged
L-C, N-5 one COST_RELATIVE_TOLERANCE (1e-12) for dominance and the copies; QueryWorkloadEntry::range_span_ms at the three span sites
L-D, N-1 From<RefreshWindow> for EvaluationGrid, used by stored_times and delivers_on; EvaluationGrid's doc covers stored times
L-5 the copy search moves to milp/equivalent_contexts.rs (MILP symmetry reduction) and returns SharedVariables; SelectableExecutions::is_root_evaluation_context replaces the closures
L-1 stages::output_references is deleted: Memo::output_groups reads the existing QueryRoot::operator_refs
L-2 the four "Stage 3 candidates" docs name Stage 2's output, PhysicalASAPMemoCandidates
L-4 PhysicalASAPMemoCandidates::is_query_time_scan
L-6 one asap_types::workload::ONE_SECOND_MS, used by EVERY_ARRIVING_ROW and the cost model
N-3 comments state rules: no issue number in delivers_on, no "as the user decided" in anchor_is_evaluation_time
N-4 planner-layering.md lists equivalent_contexts.rs; the architecture doc's Stage 2 row names stored_times, delivers_on and the candidates' TimeIndexedPlan
F1 (high): installable::tests::view_refreshes_compile_only_the_nodes_below_them (passes on main) failed: avg by (job)(quantile_over_time(0.99, data[300s])) every 1 s at relative 0.05, no raw data, had no plan diagnosed: the list was over-restrictive, main's DDSketch plan is valid (#1302: a relative bound on avg holds for values of one sign; data is declared non_negative). The avg group's exact-accumulator path reads count(quantile); a Project above the count passes no metric, so the count's group got none, and the walk and its exact-only closure marked the quantile and every sketch below it exact-only. A count's value does not depend on its input's error (error factor 0 in every metric): a consumer now passes every metric in which an input's error does not reach its output, and the closure does not cross such an input. Computing the list per path over alternatives instead was rejected: (X) is per query, and a group reached through an exact alternative and an approximate one would then admit a sketch below the exact one metric_rule::tests::a_count_reads_its_input_in_any_metric (fails before: every group exact-only); the planner test passes again
F2 (a): a stored result read only at its newest output retained 0 in Stage 3 but one output in plan_cost (m04n, m05n, m06n, m08, m10n on non-selected plans, +0.007/s) the executor's store always keeps the newest output (InMemoryDataStore::append_materialized_view keeps the rows ending at the newest end whatever retention_ms), so plan_cost's one output is right. Stage 3 now retains at least the output interval Δ of each candidate with a retention cost (ExecutionCosts::newest_output_retention_ms): its linear retention cost is taken at no less than Δ, and (R)'s least need, the greedy bound and the reported retention_ms are floored at it a_stored_result_read_at_its_newest_output_retains_one_output (retains 10 s and pays 2.01; without the floor 0 and 2.00)
F2 (b), (c): twin executions split between readers, and a shared OneEvaluation query-time Scan, are priced for every possible reader (v04n, v13n miss the plan_cost optimum by 7.3e-10/s) not exact without a formulation change (a per-reader rate, a read-window variable like $h_v$): #1335, with v04 and v05 the contract test is draft #1334
F3 (low): context copies ignored accuracy targets each query's target (metric, ε, δ, scope) is in its signature, so (K) in #1238 keeps sharing exact a_context_equivalent_to_another_shares_its_variables (rank 0.01 every 10 s and 0.05 every 20 s are not copies; fails before)
P4 (info, from main) a 60 s query at phase 30 s still runs on a 30 s evaluation grid (evaluation_grid keeps phase 0); not changed here
L-7, L-8 body: base, Removals (main's items only), delivers_on's home, renamed functions, Design scope
#1330 reuse Operator::chain_tumbling_window_width_ms lists every operator, with #1330's signature and doc; Operator::request_anchor lists every operator and every AtModifier (no _ =>), so #1330's wildcard_enum_match_arm lint on node.rs passes
#1330 R2-L2: the chain width passed through TimeShift only (matches!) decided: a Filter or Project between a chain and a TimeRange keeps each row's window end, so it keeps the chain's width, as a TimeShift does. The rule is chain_tumbling_window_width_ms() or the input's width when Operator::keeps_input_window() (exhaustive, #1330's keeps_input_time body under the name its review proposes); the windows a TimeRange selected pass through the same operators. Before, a TimeRange(5m) over Filter over a 60 s chain was costed as one window: the merge above it read 1,000 states instead of 5,000 an_operator_keeping_its_input_window_over_a_chain_or_its_windows_selects_what_the_range_does (fails before: the merge read 1000 states, not 5000); 197 set and 34 examples unchanged

For #1330: #1330 (refactor/no-silent-defaults, 273cadbeb) owns Operator::keeps_input_window and the exhaustive Operator::chain_tumbling_window_width_ms (with its wildcard_enum_match_arm lint and their tests), and is recommended to merge first. This PR keeps identical copies of both bodies so that it builds and tests on its own. Once #1330 merges, the rebase drops these copies and keeps #1330's; the conflict is textual only (the same lines in types/src/ir/operator/node.rs), and this PR's two callers in cost/src/node/usage/mod.rs and its deletion of maintained_window_ms stay.

Executor finding (not changed here, #1336): the executor does not apply a TimeShift below a query-time chain. Its window filter takes its offset from the readers above (window_read_above) while the Scan below reads the shifted range, so the two windows miss each other. A throwaway executor test, sum per job over one sample per minute, TimeRange(5m) over a 60 s chain over TimeShift(30s) over the scan: at T = 4 h it returned a = 950 where the shifted window gives 1185 (the samples at T − 4m … T − 1m only). At T = 4 h − 30 s it returned 1185. M-A does not depend on the executor; it makes Stage 3 offer such a chain only to readers at phase 30 s.

The functional reviewer's harness was re-run on 796d43503 for the m* and v* workloads. The only changes to it were three hook edits for the new names.

  • Plan and execute.
    • 37 workloads plan, and objective = plan_cost on all 37.
    • 34 of 34 compared executions are within target.
    • Every plan and cost is the same as on 813ecaef5 except v15n. There both contexts now keep their own KLL: k = 269 for the rank-0.01 query (it was k = 52, shared), which is within its band on 12 of 12 series (was 9 of 12). Its MILP has 218 variables, as with copies off; it was 120.
    • Copies still apply elsewhere with identical objectives, for example v01 166 → 90 and v03n 144 → 80 variables.
  • Enumeration oracle (cap 300,000 plans or 300 s).
    • The selected plan is the plan_cost optimum on all 18 finished workloads: m01n–m11n, v01n, v02n, v03n, v11n, v12n and v17n.
    • Model = plan_cost on every enumerated plan there. On 813ecaef5 it differed on 8 to 2,304 plans of m04n, m05n, m06n, m08 and m10n (F2 (a)). For example, m04n plan 11 was 0.04128 in the model against 0.04828 in plan_cost; both are now 0.0482833.
    • On v04n and v13n, which stopped at 300 s, F2 (b) remains (Stage 3 overprices unselected plans with split twin readers or a shared OneEvaluation scan #1335). 1,024 plans have a model above plan_cost, by up to 0.8%, and a plan 7e-10/s cheaper exists.
    • No plan has a model below plan_cost.

Timings: release build, one thread, 5 repeats per head of the same deterministic model, run side by side on pinned cores (host load average 25–33 from other jobs). The MILP size is as built.

Workload 813ecaef5 median (range) 796d43503 median (range) Variables Rows Cost per second
c16 13.8 s (11.8–15.5) 14.0 s (12.8–15.6) 3,934 → 3,934 7,909 → 7,909 0.0501500010759 → 0.0501500010759
RangeHeavy-8users, whole 11.1 s (10.4–13.1) 13.2 s (12.3–14.9) 4,777 → 4,777 10,728 → 10,728 9.52950391423e-05 → 9.52950391423e-05 (2.3e-13 relative)

The model has the same size on both heads. On 21e473690 it is also the same cost to the bit, yet a first run gave RangeHeavy 13.8 → 15.5 s. So the wall differences are host load and HiGHS's path, not a larger model.

Plan costs against 813ecaef5, with the same binary build and instrumentation for both heads:

  • The 34 tools/dag-viewer examples (accuracy ignored, as before): all plan, with the same MILP sizes; costs are bitwise equal or within 1.9e-16.
  • The 197 set:
    • 21e473690 (round-5 layering and H1) changes no cost: all 148 workloads that plan are within 1.4e-16, and none plans on one head only.
    • 796d43503 (functional fixes):
      • 142 workloads that plan on both heads are equal within 1e-9. y18 differs by 2.7e-11 relative and has one more row: the newest-output floor, F2 (a). Its plan_cost already charged that output.
      • 36 workloads change plan, all from F1. All are random-mixed workloads with avg by (...)(quantile_over_time(φ, data[100s])) at relative 0.05 over data, which is declared non_negative.
        • With MVP caps (no raw data kept), 30 workloads now plan where 813ecaef5 found no plan: random-mixed-seed-862 workloads 01–10, -x5 01–10 and -long 01–10. Before, the MILP was infeasible or stopped.

        • With default caps, 6 workloads cost less:

          Workload 813ecaef5 796d43503 Change
          random-mixed-seed-862-workload-01-defcaps 249.37/s 207.97/s −16.6%
          random-mixed-seed-862-workload-02-defcaps 333.41/s 303.08/s −9.1%
          random-mixed-seed-862-workload-03-defcaps 250.69/s (1,380 s) 230.42/s (28 s) −8.1%
          random-mixed-seed-862-x5-workload-01-defcaps 1278.91/s 1042.58/s −18.5%
          random-mixed-seed-862-x5-workload-02-defcaps 1679.11/s 1519.50/s −9.5%
          random-mixed-seed-862-x5-workload-03-defcaps 1261.47/s 1154.86/s −8.5%

          The avg of quantiles now reads DDSketch quantiles, which F1 found valid, instead of the exact quantile over raw rows.

Round-5 reviews: what changed

Abstraction, layering, functional and efficiency reviews of 796d43503. The branch is rebased onto main 59365ce80 (with #1308's tests; no conflicts), and one commit, 44a223d93, applies the fixes. Each fix's test was written first and fails on the code before it.

Finding Change Evidence
Efficiency E1 (medium): context copies stopped sharing on RollupHeavy-8users Measured first: the failing edge costs are not rounding leftovers. The edge from candidate 226 to 262 on L1 flows is 2.811002367077395e-9 − 2.8110047226529567e-9 = −2.3556e-15 in the 60 s context and −4.7112e-16 in the 300 s one, a real difference of 8e-7 relative, whose ratio 0.200000000026 carries the rounding of the large terms. Zeroing an edge cost within 1e-12 of its terms (the suggested fix) changes nothing here, so it is not in. Instead the copy check compares every cost to COST_RELATIVE_TOLERANCE of the copy context's largest cost, with α from that pair contexts_whose_small_edge_costs_differ_by_rounding_are_copies (those two edge costs, 60 s and 300 s; fails on 796d43503). L1 flows 1,526 → 786 variables (258 executions shared), http-distinct 8,510 → 4,342 (1,366), RollupHeavy-8users 15,630 → 8,003 (2,542); costs: RollupHeavy-8users and http-distinct bitwise equal to round 4's, flows within 1.5e-12 of 796d43503
Abstraction C1 / layering R5-4 One rule for "an input's error reaches the output": ErrorContract::input_error_reaches_output and ErrorMetricsOfMemo::error_metrics_allowed_on_input = (output_allowed ∩ passed) ∪ ignored, used by both exact-only loops (the closure is now .is_empty()), the edges (error_metrics_allowed_on_input_edge, per input) and readout_caps. The two field docs say the closure stops below a count's input a_counts_input_edge_from_a_readout_holds_in_a_metric_its_output_does_not_allow (count of a KLL readout at relative 0.05: MetricRule { producer_metric: Rank } before). Widening a group's allowed metrics for a count was tried and dropped (see "A count's input is not exact-only")
R1 / R5-5 most_read_ms is floored at Δ where it is built; ExecutionCosts::retention_ms_for_need floors each edge and output need, used by least_need_ms_per_execution (now over &ExecutionCosts) and least_retention_ms. newest_output_retention_ms holds Δ for every candidate that retains rows costs unchanged on the 197 set and the 34 examples
Functional N1 Root cause: NonASAPOp::map_children applied f to scalar references before the direct child, while children() lists the child first, so candidate_node (which hands out node ids by position) built q + scalar(count(q))'s Project over the count, with q as the scalar reference. map_children now applies f in children() order (Filter, Project, Aggregate, Join, Sort, SQLWindowFunc, PromqlRelabel). plan_cost also takes a node's producers in its payload's input order, whatever order the DAG lists its edges in stage3_objective::the_objective_is_the_plan_cost_on_a_scalar_operand (f07r's shape: objective 0.0360364 vs plan_cost 0.0360197 before; equal after, and equal with the plan's edges reversed, which fails without the producer order)
Functional N2 M-A's gate is right, but no reader is offered a shifted chain today (P4; frontends strip shifts below chains); said in "One refresh rule"
Layering R5-1, R5-6, R5-7, R5-9 From<RefreshWindow> for EvaluationGrid next to RefreshWindow in plan; ExecutionContext::is_outside_evaluations; QuerySignature with named fields; QueryWorkloadEntry::evaluation_grid() replaces Stage 3's free evaluation_grid
Abstraction E1, T1, S1 key_of returns each context's queries in signature order (no recomputed signatures, no expect); queries are iterated directly. The greedy bound's 1e-9 slack has a comment (leaving an execution out cannot be undone), its relaxation uses COST_RELATIVE_TOLERANCE, and dominance's absolute floor is commented. @ start() spans use range_span_ms()
K1 Not changed: see "For #1330"
Layering R5-2, R5-3, R5-8 Body: the computed_from and least_need_ms_per_execution interfaces, Removals and M4 "Before" (only main's items), reading-order paths, c16 timing, the broken formulas
Efficiency E2 (planning slower than main on some workloads) Not changed here: #1352, and the table below reviewer decision

Planning time against main and 796d43503: release asap_planner --run-until stage3, one thread, each workload pinned to its own core, 5 runs alternating the three trees, median (range); host load 5–65 from other jobs. Variables are as built; "shared" is the executions a copy takes from its original.

Workload main 59365ce80 796d43503 this head Variables main / 796d43503 / head Shared
Faster than main
c16 27.4 s (27.1–27.5) 9.9 s (9.5–10.3) 10.1 s (9.5–10.3) 6,839 / 3,934 / 3,934 992
c09 48.4 s (47.0–51.0) 21.3 s (20.3–23.0) 20.5 s (20.2–22.3) 6,830 / 3,934 / 3,934 0
RollupHeavy-8users L1 flows 3.59 s 1.60 s 0.78 s 1,173 / 1,526 / 786 0 → 258
RollupHeavy-8users L1 http-distinct 7.99 s 4.86 s 2.65 s 8,229 / 8,510 / 4,342 0 → 1,366
RollupHeavy L1 flows 5.54 s 0.72 s 0.74 s 1,157 / 772 / 772 0
RollupHeavy L1 http-distinct 8.70 s 3.09 s 3.18 s 8,149 / 4,342 / 4,342 0
RangeHeavy L1 spatial (and -8users) 0.23 s 0.10 s 0.12 s 458 / 315 / 312 0
About equal
RollupHeavy-8users, whole (bimodal: runs near 30 s and near 60 s on every tree; the round-5 efficiency review measured 30.4 s here, 32.2 s on main) 60.3 s (30.9–62.2) 62.9 s (60.9–68.3) 49.4 s (26.2–52.5) 14,627 / 15,630 / 8,003 0 → 2,542
RangeHeavy-8users, whole 9.2 s (9.0–10.6) 9.9 s (9.7–10.6) 10.0 s (9.9–10.5) 7,464 / 4,777 / 4,777 914
RangeHeavy L1 rate, sums (and -8users) 0.03–0.43 s 0.03–0.56 s 0.03–0.56 s 0
Slower than main (#1352)
RangeHeavy L1 quantiles 0.12 s (0.12–0.18) 6.77 s 6.78 s (6.76–6.82) 5,579 / 3,731 / 3,731 0
RangeHeavy-8users L1 quantiles 0.13 s 4.03 s 4.03 s 5,585 / 3,701 / 3,701 654
RollupHeavy-8users L1 http-p99 0.83 s 6.73 s 6.47 s 4,635 / 2,899 / 2,899 924
RollupHeavy L1 http-p99 2.06 s 5.00 s 4.88 s 4,694 / 2,855 / 2,855 0
random-mixed-seed-862 workload-01, default capabilities 5.21 s 8.46 s 8.45 s 3,724 / 2,535 / 2,535 0
workload-02, default 6.01 s 23.5 s 23.5 s 3,994 / 2,696 / 2,696 0
workload-03, default 2.73 s 25.3 s 25.6 s 3,659 / 2,541 / 2,541 0
-x5 workload-01, default 2.12 s 7.74 s 7.75 s 2,236 / 1,535 / 1,535 0
-x5 workload-02, default 2.28 s 13.7 s 13.7 s 2,450 / 1,656 / 1,656 0
-x5 workload-03, default 13.3 s 16.5 s 16.6 s 2,283 / 1,541 / 1,541 0

The head changes no timing beyond noise except where copies share again (E1). main selects differently on several of these, so its time buys a different plan: RollupHeavy-8users costs 2.2477e-5/s on main and 2.1459e-5/s here; of the default-capability workloads, 03 and -x5 03 cost the same on all three trees (230.42/s, 1154.86/s), while 01, 02, -x5 01 and -x5 02 cost 685.0, 1115.0, 3427.6 and 5577.8/s on main against 208.0, 303.1, 1042.6 and 1519.5/s here. The slowdown against main is in HiGHS (weak LP relaxation, little presolve; HiGHS logs in #1352), not in building the model.

These timings depend on host load. Three rows moved by up to ±50% between measurements, in both directions; the ratios hold, the absolute times do not. Release asap_planner --run-until stage3, one core per workload, runs alternating the two trees, median (range):

Workload Round-5 reviews (above, load 5–65) Round-6 functional review, 44a223d93 (load 13–47) Round 7, 0b5ec4653 (load 29–53)
RollupHeavy-8users, whole main 60.3 s, head 49.4 s main 29.80 s, head 24.44 s main 42.8 s (42.4–58.1), head 28.4 s (28.3–32.1)
RollupHeavy L1 flows main 5.54 s, head 0.74 s main 8.68 s, head 1.08 s main 6.44 s (5.70–6.94), head 0.76 s (0.76–1.05)
RollupHeavy L1 http-distinct main 8.70 s, head 3.18 s main 13.63 s, head 4.42 s main 18.1 s (18.0–19.0), head 6.19 s (6.17–7.04)

Variables and plan costs are the same in every measurement. RollupHeavy-8users is bimodal on both trees (runs near 30 s and near 42–62 s), so its row reads "about equal" to 1.5× faster depending on the run; the two L1 rows are 2.7–3.1× (http-distinct) and 7.5–8.5× (flows) faster on the head in all three.

RangeHeavy-8users plan cost, main 9.6355e-3/s → head 9.5295e-5/s. The 100× is in main's reported cost, not in the plan: main's selected_plan_cost_per_second is its MILP objective, which for this workload charges about 9.54e-3/s that plan_cost does not. Priced by main's own plan_cost, main's plan costs 9.5430e-5/s, so the head's plan is 0.14% cheaper, and the head's objective equals its plan_cost (9.52950391423e-5/s). Measured on 0b5ec4653 with a temporary per-node dump of plan_cost (not committed).

Plan costs against 796d43503, the same instrumented build of both trees, one run each (the 197 set with its capabilities, the 34 examples with accuracy ignored): 212 of 231 workloads plan on both and none on one only (19 plan on neither, as before). All 212 have the same MILP size and cost within 1e-9; 195 are bitwise equal and 17 differ by at most 2.2e-16. So no plan changes on these sets; the map_children fix changes plans only where an operator reads a scalar reference, which none of them selects (f07r is the pinned case). Copies share executions on 4 of them, as before.

Round-6 review: what changed

Abstraction and layering review of 44a223d93; one commit, 0b5ec4653, applies the fixes. Each behavior fix's test was written first and fails on 44a223d93.

Finding Change Evidence
E1-b (medium): the copy tolerance could be set by an execution the MILP had left out proportional_costs pairs only kept executions and edges whose two ends are kept (the key already makes kept equal in both contexts), so the tolerance is 1e-12 of the largest kept coefficient; it also requires $\alpha &gt; 0$ a_left_out_execution_does_not_set_the_tolerance_of_context_copies: m[5m] every 60 s and every 300 s with a left-out OneEvaluation range costing 1e12 (2e11). On 44a223d93 the contexts merged and the optimum was 2.5; now they do not, and it is 2.421. RollupHeavy-8users still shares: L1 flows 786 variables (258 executions shared), L1 http-distinct 4,342 (1,366), whole 8,003 (2,542), the same as on 44a223d93
L-1 least_need_ms_per_execution (field and function) → least_retention_ms_per_execution: it holds each execution's least retention, each need floored at Δ
N1 gap (low) ScalarExpr::map_parts maps an IN (SELECT …)'s subquery before its left side, as operator_refs lists them, so map_children follows children() order for every operator non_asap::tests::map_children_calls_f_in_children_order: every operator with scalar references (Scan, Values, Filter, Project, Aggregate, Join, Sort, SQLWindowFunc, PromqlVectorFromScalar, PromqlRelabel), each with an IN subquery; fails on 44a223d93 (Scan: 11, 12, … against 12, 11, …)
A2 plan_cost reads each node's producers from its payload (PhysicalASAPDAGNode::producers, from edge_roles, which a validated DAG's edges equal) instead of sorting the edges, so no producer sorts first silently stage3_objective::the_objective_is_the_plan_cost_on_a_scalar_operand (edges reversed)
A3 Stage 3's execution_costs builds each candidate's unretained node once and reuses it for retains_rows, Δ and the retained nodes
A4 signature indexes outputs_of[&query]: a query has an evaluation context exactly when it has an output reference
L-2 ErrorContract::input_error_reaches_output is pub(crate) and indexes its input directly (contracts are sized to children().len())
B1–B3 Body: the copies' tolerance stated as 1e-12 of the largest kept coefficient wherever it said "exact"; c16 128 s → 10.1 s; RollupHeavy-8users (whole) under "About equal" (bimodal); E2 numbers from the table; the map_children order and producers in the interface diff

Tests run on 0b5ec4653 (only those covering the changed code): cargo nextest run -p asap-types -p asap-accuracy -p asap-cost -p asap-stage3-plan-selection --lib 256 passed; -p asap-types -p asap-stage3-plan-selection -p asap-planner --tests 292 passed, 2 failed (summary_sharing::identical_ungrouped_queries_share_their_producers, runtime_install::check_compiles_reports_the_executors_error, both failing on main too); -p asap-integration-tests stage3_objective 16 passed; cargo fmt --check and cargo clippy --all-targets -D warnings on the four changed crates clean.

For #1242: both PRs change NonASAPOp::map_children/map_parts (#1242 renames it and adds column renumbering), so the PR that merges second must keep:

  • Conflicts: crates/planner/tests/main.rs needs both mod exact_only_reads; and mod reduce_aggregates;. In non_asap.rs the Filter, Join, Sort, SQLWindowFunc and PromqlRelabel arms conflict; Project and Aggregate merge without a conflict and come out right (f(child) first, then map_parts(column, f)).
  • In map_parts, f on the direct inputs before any scalar expression of the arm: Filter child, then pred; Join left, right, then pred; Aggregate child, filters, then having; Sort child, then keys; SQLWindowFunc child, args, then order_by; PromqlRelabel child, then value; Project child, then cols. In ScalarExpr::map_parts, an InSubquery's subquery before its expr.
  • feat(executor): grouped stddev/variance, corr, group, count_values, exact topk and without grouping #1242's column renumbering: map_pred(…, column, f), map_keys(…, column, f), partition_by.map_columns(column), and kept for a Scan's predicates and an Aggregate's having; this PR's &mut f calls become f, since map_parts already takes f by reference.
  • Docs: this PR's "in Self::children order … q then c" sentence moves onto map_parts, as map_children then delegates to it.
  • map_children_calls_f_in_children_order stays and must pass after the merge. map_input_columns (C::clone) does not depend on the order; the only positional caller, plan.rs:41, is the same in both PRs.

Round-7 verification

The round-6 functional and efficiency review (of 44a223d93), re-checked on 0b5ec4653; no code change, so the head stays 0b5ec4653. Release builds, review harness not committed.

Check Result
The review's adversarial copy test (F1): 10 s and 20 s contexts of one key, an unselected range alternative costing RV_BIG (half in the copy), small scan costs whose preferred alternative flips between the contexts Objective 4.5e-7, the separately optimal cost, for every RV_BIG from 1e4 to 1e15 (1e6 gave 6.0e-7 on 44a223d93). Not added as a test: it is the same case as a_left_out_execution_does_not_set_the_tolerance_of_context_copies (a left-out execution setting the tolerance), at smaller absolute costs
Enumeration oracle (plan_of + plan_cost on every enumerated selection), m/v/h/f and scalar-reference sets, 119 workloads 108 with an oracle (11 infeasible on purpose), 10.0 million selections. Objective = plan_cost on all 108 (largest relative difference 3.7e-16). The selection is the enumerated optimum on 106; v04n (6.85e-9) and v13n (4.13e-9) are the #1335 twins. Among unselected selections, the model only overprices (#1335) or differs by at most 6.6e-12 relative
Copies on against off (copies_in_equivalent_contexts replaced by the identity), 197 set + 34 viewer workloads 212 plan in both modes, 19 in neither; 195 bitwise equal, 17 within 3.6e-16; copies share executions on 4
E-a timings and the RangeHeavy-8users plan cost in "Round-5 reviews", below its timing table
L1 (low): objective scale from an unselected execution #1356

Review status

Round Head Reviews State
1–3, layering up to 4cca6a110 functional, quality, efficiency, layering applied
4 0d04e4d90, 813ecaef5 M4 and the context-doubling slowdown applied
5 813ecaef5 → 796d43503 round-4 reviews: abstraction (H1, M-A, L-A–L-D), layering (L-1–L-8, N-1–N-5), functional (F1, F2 (a), F3; F2 (b), (c) → #1335), #1330 R2-L2 applied
round-5 reviews 796d43503 → 44a223d93 (rebased onto main 59365ce80) abstraction (K1, C1, R1, E1, T1, S1), layering (R5-1–R5-9), functional (N1, N2), efficiency (E1–E7) applied, except K1 (stays until #1330 merges) and E2 (#1352, reviewer decision)
6 44a223d93 → 0b5ec4653 abstraction and layering (E1-b, A2–A4, L-1, L-2, N1 gap, B1–B3) applied
6 functional, efficiency; 7 verification 44a223d93, verified on 0b5ec4653 (no code change) functional (F1 = E1-b, fixed in 0b5ec4653), efficiency (E-a, L1, plan-cost note) F1 verified; E-a and the RangeHeavy-8users cost in "Round-5 reviews"; L1 → #1356; see "Round-7 verification"

Design scope

New file, type and field (all crate-private to Stage 3 unless noted):

  • stage3-plan-selection/src/milp/equivalent_contexts.rs. Scope: MILP symmetry reduction, which contexts' variables the MILP may share. It holds SharedVariables (per execution, edge and query, the original whose variables it takes) and the private ContextKey, ExecutionKey and ExecutionInContext it buckets contexts by. These are general: any workload asking the same queries on several grids.
  • SelectableExecutions::evaluation_context_of_query (read by evaluation_context). Scope: the Stage 3 selection index; it records which context each query is evaluated in.
  • ExecutionCosts::newest_output_retention_ms. Scope: Stage 3 costs; per candidate that retains rows, its output interval Δ, the newest output a store always keeps; read through ExecutionCosts::retention_ms_for_need, the one place a read's need is floored at it.
  • const COST_RELATIVE_TOLERANCE (milp/mod.rs). Scope: Stage 3 MILP reductions; one tolerance for "equal cost": dominance, the greedy bound's relaxation, and the copies, where it is taken of the largest cost the MILP reads in the copy context (kept executions, edges between kept ones).
  • QuerySignature (private, equivalent_contexts.rs) now has named fields: answering_executions_per_output, exact_only_groups, range_span_ms, accuracy_target_index.

New public items on existing types:

  • Operator::{chain_tumbling_window_width_ms, request_anchor, keeps_input_window} (IR). Each answers one per-operator question and lists every operator.
  • QueryWorkloadEntry::{evaluation_grid_ms, evaluation_grid, request_time_grid_ms, range_span_ms} and asap_types::workload::ONE_SECOND_MS (workload; evaluation_grid reads only the entry, so it moved from Stage 3).
  • EvaluationGrid, moved to the logical stage; From<RefreshWindow> for EvaluationGrid sits next to RefreshWindow in plan, so stages::logical imports nothing from plan. Also LogicalASAPMemoCandidates::query_must_not_read and Memo::output_groups.
  • PhysicalASAPMemoCandidates::{operator, stored_times, delivers_on (moved from PhysicalCandidate), is_query_time_scan}, and TimeIndexedPlan for PhysicalASAPMemoCandidates. Scope: Stage 2's output, physical stage; Stage 3's per-edge view delegates to it.
  • PhysicalProperties::{is_across_evaluations, has_one_reader_per_context} and ExecutionContext::{EVERY_ARRIVING_ROW, is_outside_evaluations} (the context's kind decides whether evaluation contexts share its executions).
  • The crate-private ErrorContract::input_error_reaches_output and ErrorMetricsOfMemo::error_metrics_allowed_on_input(_edge) (asap-accuracy, the metric rule): one rule for "an input's error reaches the output", used by the exact-only lists, the edges and the readout caps.
  • In stages::physical::execution_context: the free functions query_reads, queries_of_context and anchor_is_evaluation_time, used over both memo groups and plan nodes.
  • RefreshWindow::computed_from (plan), PhysicalASAPDAGNode::{keeps_raw_rows_at_ingestion, producers} (plan; producers reads only the node's payload, so plan_cost takes producer order from it instead of sorting edges) and shift_back_ms_below over alternatives.
  • asap_cost::{output_interval_ms, reads_one_batch_of, context_executions_per_second} and Consumer::QueryRequest.

The context-copy sharing is a formulation change, accepted by the reviewer (see "Decided").

Algorithm changes

A copy of an equivalent evaluation context shares the original's variables

  • Problem: c16 (topk(5, sum by (host)(rate(c[5m]))) every 60 s and every 120 s) took 107–239 s, against 15 s for either query alone; RangeHeavy-8users 222 s on seed 0.
  • Before: one $x_{\kappa,v}$, $y$, $z$ per execution in each context.
  • After: copies_in_equivalent_contexts (milp/equivalent_contexts.rs) walks the root evaluation contexts (no request anchor) in id order, buckets them by a canonical key, and within a bucket compares only costs; a context is a copy of an earlier one when their executions are of the same candidates, kept alike, with the same least need, their edges the same memo edges from producers that correspond (in the context, or the same execution outside every evaluation context; a producer in another evaluation or request context rules it out), their queries (each in its own evaluation context, SelectableExecutions::evaluation_context, wherever its answering executions run) pair up with the same output executions, exact-only groups and range span, and every cost the MILP reads (of a kept execution, or of an edge between two kept ones) is the same multiple $\alpha &gt; 0$ of the original's, to within 1e-12 (COST_RELATIVE_TOLERANCE) of the largest such cost in the copy context. A left-out execution's cost does not count: it is not in the MILP, and one costing 1e12 would otherwise allow each kept cost an error of 1 (round 6, E1-b). selection_variables gives the copy's executions, edges and queries the original's variables; the objective adds their costs on one variable. All rows stay (duplicates of the original's), HiGHS's presolve drops them.
  • Correctness: take an optimal plan with selections $S_1$, $S_2$ (own costs $A$, $\alpha B$ in the two contexts, $B$ = $S_2$ costed in the original). Give both $S_1$ when $A \le B$: the copy costs $\alpha A \le \alpha B$; $h_v$ and $r_s$, each the most of their readers' needs, equal in both copies, do not grow; a shared execution only $S_2$ read is dropped at no cost (costs are non-negative). Else give both $S_2$. So some optimum selects alike in both, and the shared-variable MILP keeps it, to the tolerance the costs are compared to: each copied coefficient within 1e-12 of the largest kept coefficient of $\alpha$ times its original, below HiGHS's own resolution once the coefficients are scaled to at most 1,000. (X)'s $a_{q,e}$ and (E)'s $t$ stay per copy; the mirrored values satisfy them. The test pins the mapping, the optimum (3.75) and the variable count, and that one cost off the multiple makes the contexts no copies.
  • Complexity: O(executions + edges + Q log Q) to key the K root evaluation contexts, then a cost comparison only against the originals of the same key (O(executions + edges) each); c16: K = 2, 23–25 ms of construction (all of the MILP's).
  • Limits: contexts with different queries, different reaching groups, or a request context below them are not merged; their doubling stays (the θ fan-out cut F4 is the follow-up).

One refresh rule for an ingestion-time stored result (M4)

  • Problem: an ingestion-time sum(TimeRange(5m)) view on a 60 s group grid ran its inputs on a 60 s pipeline and served a 60 s reader in Stage 3, while the installed plan refreshed it every 5 minutes (tumbling) and the cost model charged one output per 5 minutes; a chain over TimeShift(30s) had phase 0 in Stage 3 and 30 s in the plan.
  • Before: Stage 3 (PhysicalCandidate::delivers_on): a chain stored at every multiple of its width at phase 0, any other stored result on its group's evaluation grid; plan refresh_window; cost maintained_window_ms (the first range or chain below through filters, projections and shifts).
  • After: RefreshWindow::computed_from over TimeIndexedPlan, the plan's rule, for all three. Over candidates the producers of one input are alternatives: windows_computed_from unions their windows and shift_back_ms_below needs their shift sums to agree.
  • Correctness: the test above; no harness workload changes cost (the 197 set and the 34 examples), as round 3 found no workload selecting such a view.
  • Round 5 (M-A): stored_times uses this rule for any chain at either timing too (a query-time chain over TimeShift(30s) is stored at phase 30 s), and its group's evaluation grid for any other query-time result. Today no reader is offered such a chain: the frontends strip the shifts below a chain and re-wrap them above it (add_chain_groups), so every chain reads unshifted data, and a 60 s query at phase 30 s runs on a 30 s grid at phase 0 (P4). The gate is correct (a review-only test of a query-time 60 s chain over TimeShift(30s): stored at (60 s, 30 s), no edge to a 60 s reader at phase 0), but the gain it describes cannot happen yet, and Executor: a query-time chain over a TimeShift ignores the shift in its own window #1336 cannot be triggered by a real workload.
  • Limits: over candidates the window is the union over every admissible alternative below, which equals the selected plan's when the alternatives agree (they compute one group); a disagreement gives None (only final producers deliver).

A count's input is not exact-only (round 5, F1)

  • Problem: avg by (job)(quantile_over_time(0.99, data[300s])) at relative 0.05 over non-negative data, with no raw data kept, had no plan. Pass 2 listed every DDSketch group as exact-only for it, and (X) enforces the list.
  • Before: exact_only_groups_of_query passes to an input only the metrics allowed at the consumer's output that its contract passes. The avg group's exact-accumulator path reads count(quantile) under a Project that passes no metric, so the count group allowed none. The walk then gave the quantile group no metric (the intersection over paths), and the closure "every group below an exact-only group" covered every sketch below.
  • After: a consumer also passes every metric in which an input's error does not reach its output (error factor 0, such as an exact Count accumulator or a count measure). One function holds the rule, ErrorMetricsOfMemo::error_metrics_allowed_on_input = (allowed at the output ∩ passed by the contract) ∪ the metrics where ErrorContract::input_error_reaches_output is false; the walk, its closure (an input below an exact-only group is exact-only when that set is empty under an output allowing none) and the edges' metric rule use it, and the readout caps use input_error_reaches_output. The metrics a group's output allows (readout_caps) are not widened by a count: widening the quantile group under avg = sum / count admitted every alternative readout of it and grew models with no cheaper plan (avg of a relative quantile 62 → 455 MILP variables, random-mixed-seed-862 workload-01 875 → 1,164 and 60 ms → 12.7 s, four defcaps workloads that plan in 8–25 s did not plan within 300 s in one run on a loaded host), so a count's input edge holds only where it reads a readout directly. The intersection over paths, and over a group's alternative candidates, stays: (X) is per query, so a group reached through an exact alternative must stay exact-only below it.
  • Correctness: a count of values is the same whatever their error; the keys it groups by are copied from a source (its contract requires it). metric_rule::tests::a_count_reads_its_input_in_any_metric; installable::tests::view_refreshes_compile_only_the_nodes_below_them.

A stored result retains at least its newest output (round 5, F2 (a))

  • Problem: an ingestion-time chain read only by a stored ingestion-time TimeShift (which reads its newest window) got $h_v = 0$ in Stage 3. plan_cost charges one retained output (retained_output_count … .max(1)), and the store keeps the newest output whatever retention_ms.
  • After: ExecutionCosts::newest_output_retention_ms holds Δ, the output interval, for each candidate that retains rows. The most any consumer reads (most_read_ms) is floored at Δ once, where it is built, so the linear retention cost is taken at $\max(\text{most read}, Δ)$; each read's need is floored at Δ by one helper, ExecutionCosts::retention_ms_for_need, which (R)'s least need per execution (and so the greedy bound) and least_retention_ms use. A selected stored result retains at least Δ and pays for it.
  • Correctness: a_stored_result_read_at_its_newest_output_retains_one_output. On the 197 set only y18 changes, by 2.7e-11 relative.

Selection over execution contexts

  • Before: $x_v, y_{u,c,i}$ over candidates, rates per candidate, retention post-solve.
  • After:
    1. SelectableExecutions::new walks from each query output: the query's context is Evaluation { grid }. An input of an ingestion-time AcrossEvaluations consumer must deliver on its refresh window (IngestionPipeline); a TimeShift { at: End | Start }'s input runs on the grid of its queries' request end (start) times; an AcrossEvaluations producer runs in Materialized(u); any other ingestion-time producer in the one ingestion context EVERY_ARRIVING_ROW (each arriving row computed once, as asap_cost::computations charges it); any other in its consumer's context.
    2. cost::execution_costs: each execution costed with node_resource_usage_and_cost at executions_per_second (ingestion 1; evaluation context: evaluation_rate of the context's queries reaching the group, the @-anchored subtree once per request; Materialized(m): as often as $m$ runs, asap_cost::plan::execution_demand::executions_per_second). Edge cost = the consumer's cost in its context with input $i$ from $u$ − its own. A retaining candidate is costed at the most any of its consumers reads back (edge_retention_need_ms); $\pi_v$ = cost of one more window (chain) or second, $W^{\mathrm{mat}}_v$ = cost − $\pi_v h_v$. Baseline inputs are over the candidate's admissible producers; an ingestion-time consumer reads one batch of raw data, also from a Scan materialized at ingestion.
    3. milp: (A)–(F) and the one-consumer rule per execution; (R): $h_v \ge \mathrm{need}\cdot y$ per selected edge and $h_v \ge \mathrm{span}q\cdot z{q,v}$; a query-time scan's raw data on disk: $r_s \ge \pi_v h_v$ (or $r_s \ge d_v x_{\kappa,v}$ without $h_v$), once per source. The greedy upper bound and the strongly connected components are over executions.
    4. plan::plan_of builds one node per selected execution in Kahn order; retention_ms = the least $h_v$ (R) allows for the selection.
  • Correctness: stage3_objective pins the optimum to plan_cost (1e-9 relative) on five workloads (two chain widths without raw data, request overhead and disk on two filtered scans and on one query, the shared scan, q21). Model tests: one-consumer rule per context, a None scan once per context on two grids, a materialized chain once retaining 300 s for 1 m and 5 m readers, (E) on a cycle, the bound's left-out candidate; cost::tests::no_execution_reading_a_producer_costs_less_than_nothing; the planner test above.
  • Complexity: executions ≤ |C| × |contexts|; one node_resource_usage_and_cost per candidate, per execution, per context edge and per retaining candidate (one more). Example 3a: 1,560 variables, 2,130 rows, solved in 0.34–0.54 s over seeds 0–4 (release); timings per seed in "Review round 1: what changed".

Exact-only reads per query, constraint (X) (second commit)

  • Problem: Pass 2's exact_only_groups_of_query lists, per query, the groups it may read only exactly, while other queries may still read their sketches; PR 6's selection did not enforce it, so a relative-error query read a rank-error query's KLL, and (with PR 6's exact-target fix) an exact query read a sketch readout.
  • After: an execution $e$ query $q$ must not read (LogicalASAPMemoCandidates::query_must_not_read: its candidate has sketch parameters θ and its group is on $q$'s list; a readout's own candidate has no θ, but the states it reads are listed, as every group below a listed one is). Continuous $a_{q,e} \in [0,1]$ marks what $q$'s selected path reads: $a_{q,e} \ge z_{q,e}$, $a_{q,p} \ge y_{\kappa,p,c,i} + a_{q,c} - 1$; for a forbidden execution $a_{q,e} = 0$: $z_{q,e} \le 0$ and $y_{\kappa,p,c,i} + a_{q,c} \le 1$. $a$ exists only for executions $q$ may reach from its outputs and from which a forbidden one is reachable; it is not in the objective, so the optimum's cost is unchanged. The greedy bound leaves out of each query's trees what that query must not read, so its plan meets (X).
  • Correctness: milp::model::tests::a_query_reads_its_exact_only_groups_through_exact_candidates_only (Q0 every 10 s, Q1 every 30 s on one p90: with Pass 2's list Q1 reads the exact quantile at the output; with only the state group listed, along the edge; with no list, the readout); planner exact_only_reads::a_relative_query_reads_no_rank_sketch_of_a_query_sharing_its_groups (R3) and ..::an_exact_query_sharing_groups_with_an_approximate_one_reads_no_sketch (R2). All three fail with (X) removed and pass with it. Sketch sizing for another query's target (R2(b)) stays for PR 8.

asap-cost per context, raw rows at ingestion, shifted chains

  • computations: a node not materialized across evaluations is computed once per execution context of its consumers, per (evaluation grid, anchor) (multiplier = number of contexts), at context_executions_per_second, the rate Stage 3 uses; an @ end()/@ start() subtree once per request (Consumer::QueryRequest { at }). Tests: a_not_materialized_node_is_computed_once_per_evaluation_grid, a_range_query_computes_its_at_end_subtree_once_per_request, a_shared_one_evaluation_at_end_shift_runs_its_input_once_per_request, at_start_inputs_run_once_per_distinct_request_start.
  • Raw rows materialized at ingestion are a Scan or a TimeShift by an offset over one; both output and keep the rows of their retention_ms, which for a stored shift includes its offset (refactor(stage3): baseline selection MILP and its PhysicalASAPDAG; executor θ, native chains and a time-indexed store (#1207 PR 6) #1236's StoredThrough read), as PR 6's F1 rule keeps for any other ingestion-time view (q21: the objective was 0.326/s for a plan plan_cost charged 0.0219/s). Test: usage::tests::an_ingestion_time_view_of_raw_rows_keeps_the_rows_its_retention_covers (a 390 s retention behind a 90 s offset keeps 390,000 rows).
  • chain_tumbling_window_width_ms passes through every operator that keeps its input's window (Operator::keeps_input_window: TimeShift, Filter, Project), which also pass on tumbling_windows, so a TimeRange over a shifted, filtered or projected chain selects windows. Test: usage::tests::an_operator_keeping_its_input_window_over_a_chain_or_its_windows_selects_what_the_range_does (round 5 added Filter and Project; failed before).

PR 6 round-2 findings (measured on 1ef03212f)

The numbers below are from before review round 1; it changed z05 to 0.9301/s ("Review round 1: what changed").

With PR 6's F1 rule (an ingestion-time view keeps what its retention covers, a TimeShift's offset more) and this PR's raw rows at ingestion are kept together; a TimeShift by an offset over a Scan stays raw rows (output and keep the rows of their retention $h_v$ plus the offset), any other ingestion-time view takes PR 6's rule. PR 6's F2 (a), a query-time Scan costed for the longest reader it could have, is closed here by $h_v$ and (R): the scan's read and retention follow the selected readers. Measured with the functional review's harness on its round-2 workloads (default unit costs, executed against the exact reference):

Workload PR 6 9ec3847fc: optimum / plan_cost This PR: optimum = plan_cost The 1 m query's part
z01 count_over_time(m[1m]) every 60 s alone 0.006217 / 0.006217 0.006217/s query-time scan path, 0.0062/s
z02 z01 + max_over_time(m[1d]) every 600 s 1.037732 / 1.037732 (1 m query on an ingestion-time 60 s chain, 0.0160/s, because the scan's retention was priced for the 1-day read: 6.65/s) 0.899019/s the same scan path as z01, 0.0062/s; the scan's raw data on disk is kept once for the source at the 1-day reader's retention
z04 count_over_time(m[1m]) every 10 s alone 0.0373 / 0.0373 0.0373/s 0.0373/s
z05 z04 + max_over_time(m[1d]) every 600 s 1.073982 / 1.073982 (1 m query on a 10 s chain, 0.0517/s) 1.058982/s z04's scan path, 0.0373/s
y01 [1m] every 1 h + [1h] every 10 s 2.2565 / 2.2523 (1.9e-3) 2.2322/s

All 24 planned round-2 workloads: optimum = plan_cost (PR 6: 23 of 24); none selects an ingestion-time TimeShift view; 23 executed answer within target (z05 reads a 1-day chain the harness's 2 hours of data cannot fill; y16 is the drain-order deadlock the review notes, not run). The F1 workloads: y13 0.10455/s, y05 0.0924/s, y08 0.19813/s, y18 0.04160/s, y02 0.64055/s, z03 0.15300/s, all installed and exact. The 117 round-1 workloads (planned, not executed): optimum = plan_cost on all 98 planned, the same statuses as PR 6.

Decisions and open questions

Needs reviewer decision:

Decided:

  • Context copies share MILP variables, decided: accepted (2026-11-16). A formulation change, exact to within 1e-12 of the largest kept coefficient. copies_in_equivalent_contexts above (milp/equivalent_contexts.rs, returning the crate-private SharedVariables); the MILP for a workload with the same queries on several grids has one set of $x$, $y$, $z$ per group of equivalent contexts. Example: c16, 7,858 → 3,934 variables, optimum unchanged, about 128 s → 10.1 s (round-5 timing table; 27.4 s on main). Round 5 found and fixed a fault in it (H1): a query was put in the context of its first answering execution, which for an AcrossEvaluations candidate is Materialized(v), so two contexts whose queries read different exact-only groups counted as copies; with it, Q0 every 10 s and Q1 every 20 s that must read the exact quantile selected alike, at 9 instead of the optimum 8 (queries_reading_different_groups_select_differently_in_contexts_alike, fails before). No harness workload changed (197 set, 34 examples). Since round 5 the pairing also compares each query's accuracy target (F3), so (K) in refactor(stage3): constraint (K) per execution and metric; sign-conditioned DDSketch guarantee, classic HLL certificate from the source's distinct bound (#1207 PR 8) #1238 keeps it exact.
  • M4, decided (b) (2026-11-15): keep the 5-minute tumbling refresh and make delivers_on, Stage 3's ingestion pipeline and the cost model use the plan's rule; only readers on a grid the refresh window divides read such a view. Rejected: (a) refreshing on the readers' grid (needs a new window type).
  • R1, decided: A (2026-11-14). An instant query's input anchored with @ end() runs in that query's evaluation context, since its anchor is its evaluation time, and shares results with the plain reads on its grid, in Stage 3's contexts and in plan_cost together (asap_types::stages::anchor_is_evaluation_time; when an instant and a range query on one grid share the shift, the input runs at every evaluation of the grid, which covers the range query's request ends). Range queries and @ start() keep their per-(anchor, queries) request contexts. a06 back to 0.0360/s, objective = plan_cost. Tests: stage3_objective::an_instant_at_end_input_shares_the_evaluation_context, execution_demand::tests::an_instant_at_end_input_runs_in_the_evaluation_context (both fail without the change).
  • Review round 1 (all approved items in this head): functional F1–F5, quality docs: clean up L3 QueryExpr; add schema flow + L4 SketchExpr + system I/O #1–feat(core): model schema completeness (Schema.closed, open/closed) — Calcite DynamicRecordType pattern #15, efficiency F1 (create $h_v$ only for kept candidates; dead raw-scan retentions out of the scale), F2 (retention-aware greedy bound) and F3 (OneEvaluation-twin dominance). Efficiency F4–F6 are follow-ups.
  • D2: a not-materialized query-time scan's read is on its edges (concepts doc §3), a materialized one's per execution.
  • 2026-11-13: N1–N3 approved as written (kept in this PR):
    • N1: ingestion-time executions that are not materialized share one context (EVERY_ARRIVING_ROW, an IngestionPipeline of 1 s), as computations computes an ingestion-time node once per second whatever views read it. A producer of an ingestion-time chain is still checked against the chain's refresh window (delivers_on).
    • N2: a query-time scan's retention is costed through the per-source variable ($r_s \ge \pi_v h_v$, or $r_s \ge d_v x_{\kappa,v}$ without $h_v$), so raw data is charged once per source as plan_cost does; SolvedSelection::retention_ms is the least $h_v$ (R) allows for the selection.
    • N3: new names: milp/exact_only.rs with $a_{q,e}$ (read_by_query) and constraint (X).
  • Taken from PR 6's fixes: a query-time tumbling-window chain is built at each evaluation and read through; Materialized(m) runs as often as asap-cost runs $m$; Selection::selected_plan_cost_per_second is the optimum; (B)/(D) left out where (F)'s equality implies them; the scale in the raw-data rows.
  • Exact-only enforcement per query in its selection context: approved scope for PR 7.

Verification

On 44a223d93 (round-5 reviews), rebased onto main 59365ce80:

Scope Result Notes
cargo test --no-fail-fast for asap-accuracy, -types, -cost, the three frontends, stage0, Pass 1, Pass 2, Stage 2, Stage 3, -planner, -devtools, -integration-tests; and the same on main 59365ce80 head 1090 passed, 65 failed; main 1051 passed, 65 failed the same 65 failing names on both (devtools' todo!("#1207 PR 9") and the known planner/integration ones); none fails on the head only. Names only on main: the renamed a_query_time_result_that_is_not_materialized_has_one_consumer and doctests whose line numbers moved
New tests pass; each fails before its fix contexts_whose_small_edge_costs_differ_by_rounding_are_copies, a_counts_input_edge_from_a_readout_holds_in_a_metric_its_output_does_not_allow, stage3_objective::the_objective_is_the_plan_cost_on_a_scalar_operand
cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings clean
Timings, 197 set, 34 examples tables in "Round-5 reviews"

On 796d43503 (review round 5):

Scope Result Notes
cargo test --no-fail-fast -p asap-types -p asap-cost -p asap-accuracy -p asap-stage1-pass2-reuse-candidates -p asap-stage2-physical-optimization -p asap-stage3-plan-selection all pass (types 244, cost 45, accuracy 32, Pass 2 28, Stage 2 15, Stage 3 18) new: a_count_reads_its_input_in_any_metric, contexts_whose_queries_read_different_groups_are_not_copies, queries_reading_different_groups_select_differently_in_contexts_alike, a_stored_result_read_at_its_newest_output_retains_one_output, a_stored_chain_and_an_ingestion_time_result_are_stored_on_their_refresh_window, an_operator_keeping_its_input_window_over_a_chain_or_its_windows_selects_what_the_range_does; the copy test also checks targets
cargo test --no-fail-fast -p asap-planner -p asap-integration-tests 9 failing (planner 2, integration 7): the same 9 names as on main 59365ce80 813ecaef5 had a 10th, installable::tests::view_refreshes_compile_only_the_nodes_below_them (F1; the round-4 claim "none newly failing" compared with 4cca6a110, not main), now fixed
cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings clean
197 set, 34 examples, c16, RangeHeavy-8users tables in "Review round 5"

On 813ecaef5 (review round 4):

Scope Result Notes
cargo test -p asap-types -p asap-cost -p asap-stage2-physical-optimization -p asap-stage3-plan-selection all pass (types 159 + 77 + 8, cost 45, stage2 13, stage3 13 + 2) new: an_ingestion_time_result_is_stored_on_its_refresh_window (M4's two examples), a_context_equivalent_to_another_shares_its_variables (the copy mapping, optimum and variable count)
cargo test --no-fail-fast -p asap-executor -p asap-planner -p asap-integration-tests 10 failing, the same names as on 4cca6a110 one more than main (view_refreshes_compile_only_the_nodes_below_them, F1 of round 5)
cargo fmt, cargo clippy --all-targets on the four changed crates clean
197 set, 34 examples, multi-grid paper workloads costs unchanged (tables in "Review round 4")

On 4cca6a110 (on main 4de407569):

  • cargo fmt --check; cargo clippy --workspace --all-targets -D warnings clean.
  • cargo test --workspace: 1425 passed, 66 failed, the same 66 names as before.
  • The functional review's harness (concurrent root drains), default unit costs:
Workloads Planned Optimum = plan_cost Executed = exact reference Brute force
round 1 (116, c16 apart) 98 98 53 of 54 (x16: PR 8) never cheaper than the MILP; p29, x06, x10 time out
round 2 (25) 25 25 25 of 25 never cheaper
this PR's review (47, the a-set included) 43 43 34 of 34 compared never cheaper; x01, x03, x08, x11, x15 time out
round-2 h/k (rv2/wl) 32 32 11 of 11 compared never cheaper
r1294/wl 36 36 2 of 2 compared never cheaper

Objectives on the round-3 head (77f403791) against 33e7d6d56: equal except N-k19's set (k19_C/C2 0.3642 → 0.1542/s, a03 0.1296 → 0.1122, a04 0.1364 → 0.1146, k01/k02 0.2149 → 0.1624, k07 0.1800 → 0.1346, k09 0.0891 → 0.0717, k10 0.1103 → 0.0884, x1–x3 0.1296 → 0.1122) and p12/p13 (@ <ts> final since #1299: 1.208 → 0.0132, 1.252 → 0.0574), each = plan_cost. The layering commits change no objective on any workload.

  • The 34 tools/dag-viewer examples: objectives equal to 77f403791 within 3e-16; 37.8 s wall (release, seed 0); example 3a 21,686.84/s.
  • New tests (round 3 and layering): stage3_objective::{..._on_an_instant_and_a_shifted_range_at_end, ..._when_two_queries_read_one_anchored_group}, execution_demand::tests::an_ingestion_time_reader_of_raw_rows_kept_at_ingestion_reads_one_batch, memo_candidates::tests::an_ingestion_time_view_without_a_window_delivers_on_every_context; the N1 and N-k19 tests fail without their fixes.

Follow-ups

@zzylol zzylol added optimizer Workload optimization and plan selection refactor labels Nov 7, 2026
@zzylol
zzylol force-pushed the refactor/1207-stage3-baseline branch from 09d69f1 to 7d1bd55 Compare November 9, 2026 09:01
@zzylol
zzylol force-pushed the refactor/1207-contexts branch from 8a466a4 to 3b831f9 Compare November 9, 2026 09:35
@zzylol zzylol changed the title refactor(stage3): execution contexts x^mat, x_{κ,v}, y_{κ,u,c,i}, h_v and constraint (R) (#1207 PR 7) refactor(stage3): select over execution contexts: x_{κ,v}, y_{κ,u,c,i}, h_v and constraint (R) (#1207 PR 7) Nov 9, 2026
@zzylol
zzylol force-pushed the refactor/1207-stage3-baseline branch from 7d1bd55 to e53cf6e Compare November 9, 2026 12:11
@zzylol
zzylol force-pushed the refactor/1207-contexts branch from 3b831f9 to 6e46d1f Compare November 9, 2026 12:22
@zzylol
zzylol force-pushed the refactor/1207-stage3-baseline branch from e53cf6e to 588d1a2 Compare November 9, 2026 17:48
@zzylol
zzylol force-pushed the refactor/1207-contexts branch 2 times, most recently from f7299f5 to 4216ad0 Compare November 9, 2026 19:13
@zzylol
zzylol force-pushed the refactor/1207-stage3-baseline branch from 588d1a2 to 6e44c2b Compare November 9, 2026 21:28
@zzylol
zzylol force-pushed the refactor/1207-contexts branch from 4216ad0 to a1493df Compare November 9, 2026 21:36
@zzylol
zzylol force-pushed the refactor/1207-contexts branch from a1493df to 0816428 Compare November 9, 2026 23:55
@zzylol
zzylol force-pushed the refactor/1207-stage3-baseline branch from 6e44c2b to 0723ff6 Compare November 10, 2026 00:19
@zzylol
zzylol force-pushed the refactor/1207-contexts branch from 0816428 to e678f70 Compare November 10, 2026 00:19
@zzylol
zzylol force-pushed the refactor/1207-stage3-baseline branch from 0723ff6 to e3938ec Compare November 10, 2026 00:40
@zzylol
zzylol force-pushed the refactor/1207-contexts branch from e678f70 to 2a95559 Compare November 10, 2026 00:45
@zzylol
zzylol force-pushed the refactor/1207-stage3-baseline branch from e3938ec to db6d114 Compare November 10, 2026 01:26
@zzylol
zzylol force-pushed the refactor/1207-contexts branch from 2a95559 to 6e7a516 Compare November 10, 2026 01:26
zzylol added a commit that referenced this pull request Nov 10, 2026
… shapes (q21's TimeRange over a shifted chain fails without the #1237 fix) and execution of selected plans on synthetic data against exact evaluation (#1253 T4)
@zzylol
zzylol force-pushed the refactor/1207-stage3-baseline branch from db6d114 to c9e1579 Compare November 10, 2026 13:37
@zzylol
zzylol force-pushed the refactor/1207-contexts branch from 6e7a516 to e9cc34a Compare November 10, 2026 13:37
zzylol added a commit that referenced this pull request Nov 10, 2026
… shapes (q21's TimeRange over a shifted chain fails without the #1237 fix) and execution of selected plans on synthetic data against exact evaluation (#1253 T4)
…text's queries, dominance in its own module (#1207 PR 7)

- `crate::retention::reads_back`: every read of an execution's rows (edge
  needs and query-output spans), folded by costing, (R),
  `least_retention_ms` and the greedy bound; `ms_to_seconds` and
  `ExecutionCosts::retention_cost_per_second` for the ms-to-seconds step.
- `asap_cost::plan::execution_demand::queries_of_context`: Stage 3 and
  `computations` pick a context's queries with it.
- `milp/dominance.rs` holds `dominated_by_one_evaluation_twin`, over two
  lists per (context, logical candidate) and the selectable edges.
- `SelectableExecutions`: `SelectableEdge`, `edges_out_of` next to
  `edges_into` (used by the components, (X) and dominance), private
  fields with getters, a map of context ids.
- `raw_data_disk_rows` gives the raw-data rows to the scale and the
  constraints; `has_one_reader`; the scaled `raw_data_on_disk` doc.
- `delivers_on` compares with `own_schedule` (no `% 0`).
- asap-cost: the `Scan` arm and the retained raw rows share `raw_rows`.
…; @ start() per span (#1207 PR 7)

- `asap_cost::plan::execution_demand::query_reads`: how each query reads
  the nodes below it, per (query, anchor), over a plan or a memo.
  `consumers` (plan_cost) walks plan nodes with it, Stage 3 memo groups
  (`SelectableExecutions::readers_of`). A run in an evaluation context is
  rated by the queries reading its group in their evaluation context, not
  by every query reaching the group: an instant `m[5m] @ end()` beside a
  range `m[5m] @ end() offset 10m` cost 0.3642/s in the MILP for a plan
  `plan_cost` charged 0.1542/s. Stage 3 walks every candidate of a group,
  `plan_cost` the selected plan; the difference is documented.
- `@ start()`: `evaluation_rate` of the request schedules per span,
  summed over spans; `anchored_request_recurrence` deleted.
- `ExecutionCosts::least_need_ms_per_execution`, computed once, read by
  the greedy bound and the h_v >= need * x rows.
- The bound uses `has_one_reader`; the query-time `Scan` reads
  `raw_rows_back`; `Candidates` holds its edge; `SelectedEdge`;
  `is_across_evaluations` wrapper inlined; module docs and
  planner-layering.md list dominance.rs.
…e; one raw-row-store reader rule (#1207 PR 7)

- IR: `Operator::chain_tumbling_window_width_ms` and
  `Operator::request_anchor`; Stage 3, asap-cost and Pass 2 call them.
- Logical: `EvaluationGrid`, `LogicalASAPMemoCandidates::own_schedule`
  (an `EvaluationGrid`) and `query_must_not_read`; `Memo::output_groups`.
  `PhysicalCandidate` keeps `delivers_on` only.
- Physical: `PhysicalProperties::has_one_reader_per_context`,
  `PhysicalASAPMemoCandidates::operator`,
  `ExecutionContext::EVERY_ARRIVING_ROW`, and the context rules
  `queries_of_context`, `anchor_is_evaluation_time` and `query_reads`
  (moved from asap-cost, which keeps depending on types only).
- Workload: `QueryWorkloadEntry::evaluation_grid_ms` and
  `request_time_grid_ms`.
- Plan: `PhysicalASAPDAGNode::keeps_raw_rows_at_ingestion` (a `Scan`, or a
  `TimeShift` without `@` over one), with `asap_cost::reads_one_batch_of`:
  an ingestion-time reader of a raw-row store reads one batch, in Stage 3
  and now in `plan_cost` too (it read the whole store there); the cost
  model's raw-row rule uses the same predicate.
- asap-cost: `output_interval_ms` is public; Stage 3's Δ of π_v calls it.
- Stage 3: `SelectableExecutions::reads_back` and `Reader`; the
  `is_across_evaluations` wrapper and `is_query_time_scan`'s memo walk
  replaced by `PhysicalASAPMemoCandidates::operator`.
…ingestion reads one batch; two queries reading one anchored group (#1207 PR 7)
…riginal's variables (#1207 PR 7)

The same queries on several grids (one user every 60 s, another every
300 s) give one evaluation context per grid with the same executions and
edges, each cost a fixed multiple of the other's. The MILP chose each
copy freely, so HiGHS branched over both: c16 130 s, RangeHeavy-8users
222 s on seed 0.

copies_in_equivalent_contexts finds such copies (same candidates, kept
alike, same memo edges from corresponding producers, paired queries with
the same outputs, exact-only groups and span, costs proportional), and
selection_variables gives a copy's executions, edges and queries the
original's x, y and z. Exact: some optimum selects alike in both copies
(give both the cheaper one's selection; retention and raw data do not
grow). Plan costs are unchanged; c16 drops to about 10 s.
…tored result (#1207 PR 7)

Three rules said when an ingestion-time stored result refreshes and
disagreed: Stage 3 (own_schedule: a chain at phase 0, anything else its
group's evaluation grid), the installed plan (refresh_window: a chain
(w, s mod w), anything else the union of the windows below it) and the
cost model (maintained_window_ms: the first range or chain below). An
ingestion-time sum(TimeRange(5m)) read every 60 s ran its inputs on a
60 s pipeline and served the 60 s reader, while the plan refreshed it
every 5 min; a chain over TimeShift(30s) had phase 0 in Stage 3 and
30 s in the plan.

The plan's rule is the one rule: RefreshWindow::computed_from over any
TimeIndexedPlan. PhysicalASAPDAG::refresh_window, Stage 3's ingestion
pipeline context, the stored times delivers_on checks
(PhysicalASAPMemoCandidates::stored_times; delivers_on moves to the
candidates, whose data it reads) and both cost paths (plan_cost and
Stage 3's ingestion_window_ms, its width) use it. The candidates
implement TimeIndexedPlan; Stage 3's per-edge Candidates delegates to
it. shift_back_ms_below follows every alternative producer and needs
them to agree. maintained_window_ms and refresh_windows are deleted.
… stored-times rule; review fixes (#1207 PR 7)

A query is evaluated in its own evaluation context, but the context-copy
search put it in the context of its first answering execution. With an
AcrossEvaluations candidate listed first that is Materialized(v), so two
evaluation contexts whose queries read different exact-only groups had
empty query lists, matched, and shared their variables: Q0 every 10 s
and Q1 every 20 s that must read the exact quantile then selected alike,
at 9 instead of 8. SelectableExecutions now records each query's
evaluation context in new (evaluation_context), the copy search reads
it, and selection_variables asserts that every shared z is a real
(query, answering execution) pair.

The copy search moves out of dominance into milp/equivalent_contexts.rs
(symmetry reduction) and returns SharedVariables. Contexts are bucketed
by a canonical key (candidates, kept, least need, in-edges and query
signatures), so only the cost proportionality check compares two
contexts; the out-edge and input-count checks it implied are a
debug_assert. Dominance and the copy search share one relative
tolerance (COST_RELATIVE_TOLERANCE).

stored_times uses RefreshWindow::computed_from for any chain, at either
timing (a query-time chain over TimeShift(30s) is stored at phase 30 s,
as at ingestion time), and its group's evaluation grid for any other
query-time result; own_schedule is deleted. EvaluationGrid gets
From<RefreshWindow>, which delivers_on uses.

A Filter or Project between a chain and a TimeRange keeps the chain's
tumbling-window width in the cost model, as a TimeShift does: the rule is
Operator::keeps_input_window (exhaustive), which also passes the windows
a TimeRange selected through them. Operator::chain_tumbling_window_width_ms
and Operator::request_anchor list every operator.

Also: QueryWorkloadEntry::range_span_ms at the three span sites;
Memo::output_groups reads QueryRoot::operator_refs and output_references
is deleted; PhysicalASAPMemoCandidates::is_query_time_scan; one
ONE_SECOND_MS in asap_types::workload; docs name Stage 2's output where
they said Stage 3 candidates; comments state rules, not process.
… output; targets in context copies (#1207 PR 7)

avg by (job)(quantile_over_time(0.99, data[300s])) at relative 0.05
over non-negative data had no plan without raw data: Pass 2 listed every
DDSketch group as exact-only for it, and Stage 3 now enforces the list
(X). The avg's exact-accumulator path reads count(quantile): a Project
above the count passes no metric, so the count's group got none, and the
walk and its closure marked the quantile and every sketch below it
exact-only. A count's value does not depend on its input's error (error
factor 0 in every metric), so a consumer now passes every metric in
which an input's error does not reach its output, and the exact-only
closure does not cross such an input. The DDSketch plan is valid: #1302
made a relative bound on avg hold for values of one sign, and data is
declared non-negative.

A store always keeps a stored result's newest output, and plan_cost
charges at least one retained output, but Stage 3 let a stored result
read only at its newest output retain 0 (an ingestion-time chain read by
a stored ingestion-time TimeShift: 0.007/s apart). Each candidate with a
retention cost now retains at least its output interval
(ExecutionCosts::newest_output_retention_ms): the linear retention cost
is taken at no less than one output, (R)'s least need, the bound and the
reported retention are floored at it.

Context copies also compare each query's accuracy target (metric, ε, δ,
scope), so the accuracy constraint (K) holds alike in both copies: the
same queries at rank 0.01 every 10 s and rank 0.05 every 20 s are not
copies.
… to the context's largest cost, one metric rule per input, plan children order (#1207 PR 7)

- Context copies compare costs to COST_RELATIVE_TOLERANCE of the copy's
  largest cost, alpha from that pair: a small edge cost, a difference of
  large costs, keeps their rounding (RollupHeavy-8users flows: -2.36e-15
  and -4.71e-16, ratio 0.200000000026), which stopped copies sharing.
- One metric rule per input: ErrorContract::input_error_reaches_output and
  ErrorMetricsOfMemo::error_metrics_allowed_on_input, used by the exact-only
  walk and closure, the edges and the readout caps; a count's input edge
  from a readout holds in any metric.
- Newest-output floor applied once: most_read_ms floored where built,
  ExecutionCosts::retention_ms_for_need for edge and output needs.
- Operator::map_children applies f in children() order, so a plan's
  Project over q with scalar(count(q)) keeps q as its child; plan_cost
  takes a node's producers in its payload's input order.
- Layering: From<RefreshWindow> for EvaluationGrid in plan;
  ExecutionContext::is_outside_evaluations; QuerySignature with named
  fields; QueryWorkloadEntry::evaluation_grid; exact-only field docs.
- Abstraction: key_of returns queries in signature order; tolerance
  comments; range_span_ms for @ start() spans.
@zzylol
zzylol force-pushed the refactor/1207-contexts branch from 796d435 to 44a223d Compare November 15, 2026 15:54
… compare kept costs only, IN subquery order, producers from the payload (#1207 PR 7)

- E1-b: proportional_costs pairs only kept executions and edges between
  kept ones, so a left-out execution no longer sets the copy tolerance;
  alpha must be positive. Test: a left-out range costing 1e12 merged two
  contexts (optimum 2.5 instead of 2.421).
- N1 gap: ScalarExpr::map_parts maps an IN's subquery before its left
  side, as operator_refs lists them; a test checks map_children follows
  children() for every operator with scalar references.
- L-1: least_need_ms_per_execution -> least_retention_ms_per_execution.
- A2: plan_cost reads producers from PhysicalASAPDAGNode::producers
  instead of sorting edges. A3: unretained nodes built once. A4:
  outputs_of[&query]. L-2: input_error_reaches_output is pub(crate) and
  indexes directly.
@zzylol
zzylol marked this pull request as ready for review November 16, 2026 00:45
@zzylol
zzylol merged commit 3e434c7 into main Nov 16, 2026
8 of 9 checks passed
This was referenced Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Workload optimization and plan selection refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant